How to count down on 60 minutes, do something every hour | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

How to count down on 60 minutes, do something every hour

I want to do something every hour. It does not need to be very accurate, 1 to 5 seconds is ok. Can I use a timer with a interval of 3600.000ms? The proces may not be depend on other process in the computer or the use of the UI. Currently I use a timer that elapses every 900 ms. Than I calculate TimeLeft = TimeSpan.FromHours - (Date.Time.Now - BeginTime); if (TimeLeft <= TimeSpan.Zero) { do something } but this calculation fails once a day. What method would you use ? Timer or calculation, something else. Why is the calculation failing ? What can I do to improve this ?

1st Feb 2022, 8:17 AM
sneeze
sneeze - avatar
1 Answer
+ 5
I haven't had problems with longer running timers, in console applications. I would probably just set an interval of 1 hour :) That is, if this timer is running in the context of a bigger application. If not, I'd run the code as a windows scheduled job, cronjob, or a time triggered azure function. Something like that.
1st Feb 2022, 8:47 AM
Schindlabua
Schindlabua - avatar