What is the correct timer used to wait for long periods (24 hours or more)?

What is the best way to just wait 24 hours +/- 1 second. I know that Threading.Sleep for a long time is not accurate and may vary depending on the processor load.

I see that System.Timers.Timer allows you to create a time event. How can I use this to just wait 24 hours?

private void myTest{ // SET SOMETHING UP m_theTimer = new System.Timers.Timer(); m_theTimer.Elapsed += new ElapsedEventHandler(OurTimerCallback); const int hrsToMs = 60 * 60 * 1000; m_theTimer.Interval = TestPeriodHours * hrsToMs; m_theTimer.Enabled = true; //---->want to wait for 24 hours<------ // RESUME TEST HERE VerifySomething() } public void OurTimerCallback(object source, ElapsedEventArgs e) { Console.WriteLine("Received a callback, the time is {0}", e.SignalTime); } 
+4
source share
5 answers

Perhaps Quartz.NET is what you are looking for. http://quartznet.sourceforge.net/ .

+3
source

+/- 1 second? I would use a hybrid approach.

Use the task scheduler to start the program at 23:58. Then skip this program for 2 minutes or less until you reach the exact moment, and then do the work.

If you don't need 1 second accuracy, you can simply use the task scheduler yourself, in 24-hour mode.

+2
source

Assuming you are in the form of a win.

Declare a datetime variable and assign a value to the form_load event. Create a timer and fire the event every second. Check the datetime variable in the timer event. if the time interval is 1 hour, call your main method.

0
source

I think you can use a combination of ManualResetEvent with a timer.

0
source

Perhaps this is better if you really want to use a timer.

  private void myTest{ // SET SOMETHING UP bool bTimer_Expired = true; m_theTimer = new System.Timers.Timer(); m_theTimer.Elapsed += new ElapsedEventHandler(OurTimerCallback); const int hrsToMs = 60 * 60 * 1000; m_theTimer.Interval = TestPeriodHours * hrsToMs; m_theTimer.Enabled = true; //Wait for call back to set flag that the elapsed time has expired while(!bTimer_Expired)Sleep(1000); //---->want to wait for 24 hours<------ // RESUME TEST HERE VerifySomething() public void OurTimerCallback(object source, ElapsedEventArgs e) { bTimer_Expired=true; Console.WriteLine("Received a callback, the time is {0}", e.SignalTime); } 

But I'm a little vague why ManuelResetEvent doesn't make the job easier ....

0
source

All Articles