Pankaj
Pankaj

Reputation: 1524

Event to detect System wake up from sleep in C#

I need to detect the system power state mode. To be precise, I need an event which fires up when windows 7 wakes up from sleep. I am already using:

SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged;

But the problem with this event is that it is raised up four times: possibly when computer goes into sleep mode and after computer wakes up. I want an event which is raised at computer wake up only. Is there any event for this?

Upvotes: 41

Views: 35208

Answers (2)

Marcus
Marcus

Reputation: 8649

SystemEvents.PowerModeChanged += OnPowerChange;

private void OnPowerChange(object s, PowerModeChangedEventArgs e) 
{
    switch ( e.Mode ) 
    {
        case PowerModes.Resume: 
        break;
        case PowerModes.Suspend:
        break;
    }
}

You should probably read this: http://msdn.microsoft.com/en-us/library/microsoft.win32.systemevents.powermodechanged.aspx

Upvotes: 63

Moo-Juice
Moo-Juice

Reputation: 38825

You need to inspect the Mode property of the PowerModeChangedEventArgs that is passed to the event.

From MSDN:

  • Resume The operating system is about to resume from a suspended state.

  • StatusChange A power mode status notification event has been raised by the operating system. This might indicate a weak or charging battery, a transition between AC power and battery, or another change in the status of the system power supply.

  • Suspend The operating system is about to be suspended.

Upvotes: 11

Related Questions