Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to schedule tasks in C# [closed]

Tags:

c#

.net

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.

like image 779
Whiteknight Avatar asked Dec 29 '25 11:12

Whiteknight


1 Answers

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).

like image 125
Michael Haren Avatar answered Dec 31 '25 02:12

Michael Haren



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!