Tasawer Khan
Tasawer Khan

Reputation: 6148

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.

Upvotes: 9

Views: 5031

Answers (1)

Eivind T
Eivind T

Reputation: 1059

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

Upvotes: 18

Related Questions