tags:

views:

48

answers:

1

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.

+1  A: 

How soon after boot does your service start? The Windows Time service can adjust the clock to get it in sync with a domain controller. Your DateTime.Now value might be taken before that happens. Diagnose this first by entering the BIOS at boot-up and checking the clock. Fix it with a service dependency.

Hans Passant
What do you mean by service dependency? Look, I am doing this on a remote server, and do not have the posibility to do as you suggested.
Markus
Dependencies tab of the service properties. If you can't reach the keyboard then surely the local admin can?
Hans Passant
Could you please give me an example of service on which the current service might be dependant on? Is the Net Logon service appropriate here?
Markus
I don't know enough about your service. But I'd try Windows Time first.
Hans Passant
Ok, I will try. Thank you for your effort.
Markus