Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple method is called again once completed

Tags:

performance

c#

Background:

I have a method that does some processing that I would like to be called over and over again. The method can take between 50 - 200 ms to complete.

Currently I have a System.Timers.Timer that calls the method when it elapses every 250 ms. I feel I I can make this more efficient by having the method "trigger" itself once it has completed.

Notes:

  • Performance is key.
  • Two of these methods should not be called at the same time, or overlap.

I would like avoid simply using a while loop that calls the method. It should call itself asynchronously perhaps using an event?

Question:

How can I have the method "trigger" itself once it has completed?

like image 872
Ryan R Avatar asked Dec 31 '25 11:12

Ryan R


1 Answers

You could perform your while loop in a separate thread of execution, as long as you take the necessary multi-threading precautions with things such as locks, condition variables, wait handles, and the like.

  • C#'s Task Parallel Library could be suited to this job. But as Reed said, be sure to use the LongRunning creation option. There's an example of this method posted by Reed here.
  • Or use a Thread object directly. SLaks posted an example of this method here.

If you're working with a WinForms application, you may want to look into using a BackgroundWorker.

like image 174
Ryan Stecker Avatar answered Jan 03 '26 02:01

Ryan Stecker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!