Scheduling running a method at a certain time. Scheduling running a method at a certain time. multithreading multithreading

Scheduling running a method at a certain time.


If you want to "schedule" a method to do something at a predetermined time, there are a number of ways to do that. I would not use Thread.Sleep() because that would tie up a thread doing nothing, which is a waste of resources.

A common practice is to use a polling method that wakes up on a regularly timed schedule (let's say once a minute) and review a shared list of tasks to perform. System.Timer can be used for the polling method:

aTimer = new System.Timers.Timer(10000);aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);

The OnTimedEvent method can contain code that maintains a collection of "tasks" to perform. When a task's time comes up, the next run of the Timer will cause it to execute.