Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop a timer during hibernate/ sleep mode in C# winform application?

I have an application which does a specific task after some time (controlled by a timer). But whenever I start PC after hibernate that application runs. This means that timer keeps running during hibernation for atleast one tick. How can I avoid this.

like image 513
Tasawer Khan Avatar asked Jan 01 '11 13:01

Tasawer Khan


1 Answers

You can handle the SystemEvents.PowerModeChanged event to stop the timer when the machine is suspending and start it again when it is resuming.

 SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged;

...

 void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)
    {
        if (e.Mode == PowerModes.Suspend) PauseTimer();
        else if (e.Mode == PowerModes.Resume) ResumeTimer();
    }
like image 191
Eivind T Avatar answered Oct 27 '22 09:10

Eivind T