开发者

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜