Exact fire event at the end of an hour in C # Timer

I want the tick event to fire every hour exactly at the end of the hour. E.g. it should point at 8 am , then at 9 am , then at 10 am , etc. It is just that I need to set Interval to 3600000 .

The problem is how to determine when I need to start the timer? I am creating a tool that will run in the system tray from the moment a user logs in.

+4
source share
5 answers

Please do not create a program that does nothing but empty memory. What is the Windows Task Scheduler designed for? Run your program every hour from such a task.

http://msdn.microsoft.com/en-us/library/aa384006%28v=VS.85%29.aspx

Here is an example:

  • Go to Start-> Programs-> Accessories-> Scheduled Tasks.
  • In the right part, click "Add task ..".
  • Select an executable file.
  • Click the "Trigger" tab.
  • Create a trigger with the following selection:

.

 Run Daily Start today at 8:00 am Repeat every 1 Hour 

I'm sorry that I can not provide screenshots, since I am running the German version of Windows 7.

+9
source

Maybe something is bad in the code, but the idea is this:

  public void InitTimer() { DateTime time = DateTime.Now; int second = time.Second; int minute = time.Minute; if (second != 0) { minute = minute > 0 ? minute-- : 59; } if (minute == 0 && second == 0) { // DoAction: in this function also set your timer interval to 3600000 } else { TimeSpan span = new TimeSpan(0, 60 - minute, 60 - second); timer.Interval = (int) span.TotalMilliseconds - 100; timer.Tick += new EventHandler(timer_Tick); timer.Start(); } } void timer_Tick(object sender, EventArgs e) { timer.Interval = 3600000; // DoAction } 

Edit: as @smirkingman suggested, I deleted a few milliseconds due to the latency of starting and starting the project of this application: timer.Interval = (int) span.TotalMilliseconds - 100;

+4
source

I think it would be easier if you set the timer every, say, a minute, and this timer can check the system clock, when the desired time is less than or equal to the system time, you can simply run actions (in this example, with a 1 minute maximun error )

You can improve it if you make the dinamyc timer interval, for example, if you check the time and still have half an hour left, you can set the interval for 15 minutes, reduce the nex time to 5 minutes, and so on, you check the clock once per second, eg.

NTN

+1
source

Here is how I did it. The Tick event fires every 20 seconds. Just change the protocol == "xxx" for any time during which you want the event to fire. If you need events scattered over several hours, just make the interval timer longer. Simple and efficient.

 private void timer1_Tick(object sender, EventArgs e) { DateTime Time = DateTime.Now; int minutes = Time.Minute; if (minutes == 00) //FIRE ON THE HOUR { DO THIS } if (minutes == 15) //FIRE ON 1/4 HOUR { DO THIS } if (minutes == 30) //FIRE ON 1/2 HOUR { DO THIS } if (minutes == 45) //FIRE ON 3/4 HOUR { DO THIS } } 
+1
source

Instead of shooting the timer once an hour, it might be more appropriate to start the timer once a minute and check if it is still time.

The only problem with this is the worst lag - 59 seconds. If you want him to shoot at exactly one hour (at 10 a.m.), you may need to play a little at intervals for the first time so that you line up.

0
source

All Articles