Assuming I have a windows service which has a timer that is set to run every 6 hours, I would expect it to fire 4 times a day. Let's say : 0000, 0600, 1200 1800. (Military time, same as 00:00, etc...)
If the system goes to sleep at 1000, and wakes at 1700, what happens?
I've noticed that when the computer goes to sleep, it doesn't fire the OnPause
or OnContinue
methods.
If anyone can shed some light on the behaviour of the system in the above cases, It'll be great know.
Cheers, and thanks in advance.
When computer is under sleep mode, all programs get suspended. Hence, your program live stock will not be running.
What is a sleep timer? A sleep timer is a function that dictates how long your computer will sit idle before going into power-saving mode.
To make sure sleep mode is enabled, follow these steps: Step 1: Press WIN + I on your keyboard to launch Settings. Step 2: Select System from the menu tiles. Step 3: Make sure you're in the Power and Sleep tab.
As has already been mentioned in the comments: Use the task scheduler instead of timers. See this article for further discussion.
The question of how timers behave during sleep is still interesting, though. In the specific case you mention, option 2. will be the observed behavior. The timer will fire on resume because it missed an event during sleep and subsequently start counting from 0 again The next time it fires will thus be at 23:00. This answer has sample code to support the observation.
In case you want to detect when the system sleeps and resumes, subscribe to the SystemEvents.PowerModeChanged
event. This event's PowerModeChangedEventArgs
contains a Mode
, of which you are interested in Suspend
and Resume
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With