简体   繁体   中英

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.

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();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM