views:

64

answers:

3

My C#-programm has a windows.forms.timer that fires all 24h for a background task. During a day I put my Vista 64bit several times in stand-by mode (never switch it off). It seems that after wake-up the timer is sort of reset.

A: 

During standby, typically memory is stored as-is, then recalled when you wake the machine, which might cause your timer to either not tick during the stand-by, or have imprecise behavior from what you intend.

byte
Thanks, this seems obvious, you are right. If I halt my system for 12h each day, the timer will run out like every two days.
Henry99
+1  A: 

The Timer class is very relaxed about raising its Tick event. Internally, inside the Windows code, when the timer is due it only sets an internal flag, somewhat akin to "ought to deliver WM_TIMER". That doesn't actually happen until nothing important needs to be done by the message loop. Any message gets higher priority than WM_TIMER.

When the Windows Forms message loop calls GetMessage(), the function checks if anything needs to be returned. If the answer is "nothing" and the flag is set, it supplies WM_TIMER. And you'll get the Tick event.

A couple of consequences from that: you can never use the Tick event to keep track of time. That will inevitably fall behind. You can never get the Tick event twice in a row, it doesn't catch up. But relevant to your question: the message loop isn't pumping when the machine goes in stand-by, nothing special happens.

Hans Passant
+2  A: 

Your timer shouldn't be set to fire every 24 hours, it should be set to fire every few seconds, or minutes, and check the time. If the time is greater than, or equal to, the time you want your task to occur, perform the task.

raven
Cool idea, thanks
Henry99