.NET Framework Bookmark and Share   
 index > .NET Base Class Library > Event on specific time
 

Event on specific time

I would like a piece of code to run when the clock strikes 10:00:00, 11:00:00, etc.
I would also like another piece of code to run 10:00, 10:10, 10:20, 10:30, 10:40, etc.

Is there an event that I can attach a callback function to?
Hobz
Windows is actually quite accurate in firing timer events at the correct time. Set your interval so the next event occurs at the correct time.

System.Timers.Timer Tmr = new System.Timers.Timer();

System.DateTime NextTime;

int Tmr_Elapsed_Count;

private void Tmr_Elapsed(object sender, System.Timers.ElapsedEventArgs e)

{

Tmr.Stop();

Sub1();

if (Tmr_Elapsed_Count % 10 == 0)

{

Sub2();

}

Tmr_Elapsed_Count += 1;

NextTime = NextTime.Add(new TimeSpan(0, 10, 0));

Tmr.Interval = NextTime.Subtract(System.DateTime.Now).Milliseconds;

Tmr.Start();

}

  • Edited byJohnWein Wednesday, December 10, 2008 2:54 PMAdded code sample.
  • Marked As Answer byHobz Thursday, December 11, 2008 12:21 PM
  •  
JohnWein
You'll need to use a timer (set the interval to 60000 which is 1 min) and every minute check to see if it's time to run one of your pieces of code.
Curtis - OH
Tough!

I was hoping to get events that fired exactly OnTimeChanged().
I mean, I could be unfortunate and check if the time is right at 09:59:58, and first get notified at 10:00:58.

So a reasonable solution would be to constantly monitor the time in a thread, and when the time is right, run the pieces of code?
Here I mean reasonable in terms of system resources and general code practice.
Hobz
This may or may not be relevant depending on what exactly you are trying to do, but Windows Scheduled Tasks may be ideally suited for the type of thing you are trying to do. Can you factor out the work you are trying to do into a few simple Console applications (you could still use a common class library) and simply run the appropriate applications at the correct time through the Scheduled Tasks?
  • Edited bydm_ntm Wednesday, December 10, 2008 2:25 PMtypo
  •  
dm_ntm
Windows is actually quite accurate in firing timer events at the correct time. Set your interval so the next event occurs at the correct time.

System.Timers.Timer Tmr = new System.Timers.Timer();

System.DateTime NextTime;

int Tmr_Elapsed_Count;

private void Tmr_Elapsed(object sender, System.Timers.ElapsedEventArgs e)

{

Tmr.Stop();

Sub1();

if (Tmr_Elapsed_Count % 10 == 0)

{

Sub2();

}

Tmr_Elapsed_Count += 1;

NextTime = NextTime.Add(new TimeSpan(0, 10, 0));

Tmr.Interval = NextTime.Subtract(System.DateTime.Now).Milliseconds;

Tmr.Start();

}

  • Edited byJohnWein Wednesday, December 10, 2008 2:54 PMAdded code sample.
  • Marked As Answer byHobz Thursday, December 11, 2008 12:21 PM
  •  
JohnWein
Just check the hour and minute and ignore seconds/milliseconds. Windows is pretty accurate down to the second. Also use DateTime.Now.Minute >= TimeToRun.Minute just in case the event happens to be a minute late.
Curtis - OH

You can use google to search for other answers

Custom Search

More Threads

• Server configuration
• How to read individual words from a file
• How to get number of idle processors?
• Building Business Logic Layer
• How do I find the public properties of a Type?
• CustomAttributeData.GetCustomAttributes finding Attributes that Type.GetCustomAttributes does not
• Error in Encryption and Decryption
• System.IO Serial Class: Detecting (USB Virtual Serial) disconnection
• How do i obtain an MCSD
• When does a shared class get initalized, and who init's it?