I'm developing a C# program that needs to be able to schedule a variety of asynchronous tasks at different times. I need a solution that activates every second to check for tasks to execute, and eats up as few resources as possible. It also needs to scale well to a large number of tasks. Which of these is better to use, or is there any ?
1) Using a System.Timers.Timer object with a 1 second elapsed time to trigger an event that executes tasks, or
2) Using a separate thread that sleeps for 1 second and then executes tasks when it wakes up, or
3) Something else entirely.
System.Threading.Timer is extremely lightweight. You could create a separate timer for each task so that the timer comes due when the task is supposed to execute.
Internally, I believe that the system will keep track of what timer is due next so it only has to monitor one timer at any given time (source/similar question).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With