I'm using System.Timers.Timer in my WPF application. I want to understand how Timer does behave, after Computer is hibernated, and sleep. I'm getting some weird issues with my application, after computer is getting resumed from hibernate.
How should I handle timers, and how do they behave when computer is in hibernate/sleep mode?
I have a midnight timer which should work each midnight to reset the default values on UI.
Here is the code that creates the timer:
private void ResetMidnightTimer()
{
// kill the old timer
DisposeMidnightTimer();
_midnightTimer = new Timer();
// scheduling the timer to elapse 1 minute after midnight
_midnightTimer.Interval = (DateTime.Today.AddDays(1).AddMinutes(1) - DateTime.Now).TotalMilliseconds;
_midnightTimer.Elapsed += (_, __) => UpdateRecommendedCollectingTime();
_midnightTimer.Enabled = true;
_midnightTimer.Start();
}
On UI page's contructor, I call the method which calls ResestMidnightTimer() and creates the timer de facto. After that the timer just waits for the night.
When the night time (actually it is the 12:01 AM) comes, the timer works, resets the default values as expected and then disposes existing timer. Finally it creates a new midnight timer for next day. But if I try to hibernate the computer during that day, the midnight timer won't work and won't reset the default values.
Is that because while hibernating it just postpones the event handling by the same amount of time it was hibernated?