Planning to launch a method at a specific time.

This is a pretty simple problem, but every time I try to find the answer, it keeps showing things about Windows scheduled tasks, and it doesn't.

Say my program is basically like this:

void StartDoingThings() { //At Specific System.DateTime DoSomething() //At another specific System.Datetime DoSomethingElse() } 

What I put instead of these comments so that these methods run on separate dates.

I could use Thread.Sleep () or even System.Threading.Timers and calculate the intervals based on (DateTimeOfEvent - DateTime.Now), but is there anything to be said (assuming the program is still running): Q 9: 30:00 AM on 11/30/2012, do the DoAnotherThing () method?

+6
source share
1 answer

If you want to β€œplan” a way to do something at a given time, there are several ways to do it. I would not use Thread.Sleep() because it would tie the thread without doing anything, which is a waste of resources.

A common practice is to use the polling method, which wakes up on a regular schedule (let them speak once a minute) and looks at the general list of tasks to be performed. System.Timer can be used for the polling method:

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

The OnTimedEvent method may contain code that supports a collection of "tasks" to execute. When the task comes, the next timer start will cause it to execute.

+4
source

All Articles