Timer in windows service

Posted by Markus on Stack Overflow See other posts from Stack Overflow or by Markus
Published on 2010-03-22T19:02:50Z Indexed on 2010/03/22 19:51 UTC
Read the original article Hit count: 305

Filed under:

Hi. I have an issue with System.Threading.Timer. I am scheduling some actions using a time in a windows service. The timer starts executing the callback after a specified dueTime period. The windows service starts up after reboot automatically. However, I have observed a strange thing after a system reboot- the callback method starts executing itself 3 or 4 minutes before the specified period. What might be the reason for such behavior?

Here is the sample code:

TimeSpan timeToWait = this.StartTime - DateTime.Now;
Int64 msToSleep = (Int64)Math.Round(timeToWait.TotalMilliseconds);
_timer = new Timer(callback_method, null, msToSleep, MinutesScheduledInterval * 60000);

where _timer is a member variable, StartTime - the time when the timer should first fire.

© Stack Overflow or respective owner

Related posts about .NET