38

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?

Ian R. O'Brien
  • 6,682
  • 9
  • 45
  • 73
Pankaj
  • 1,446
  • 4
  • 19
  • 31
  • 5
    `PowerModeEventArgs` has a property called `Mode` that you can use to say what state its returning to – Sayse Aug 13 '13 at 10:05
  • Okay .. let me try with this.... let you know :) – Pankaj Aug 13 '13 at 10:07
  • possible duplicate of [How can I know when Windows is going into/out of sleep or Hibernate mode?](http://stackoverflow.com/questions/228288/how-can-i-know-when-windows-is-going-into-out-of-sleep-or-hibernate-mode) – Ian R. O'Brien Aug 05 '14 at 13:24

2 Answers2

61
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

Marcus
  • 8,230
  • 11
  • 61
  • 88
  • 7
    Don't forget to unsubscribe the event: `SystemEvents.PowerModeChanged -= OnPowerChange;` – ramaral Nov 06 '18 at 22:36
  • 2
    Doesn't seem to work with the "new" Connected/Modern Standby modes... Possible substitute in this question: https://stackoverflow.com/questions/20119257/connected-standby-notification-for-a-w8-service/55676206#55676206 – Oded Ben Dov Apr 14 '19 at 14:11
  • @ramaral at what point the even listener needs to be removed and why exactly? – Adil Malik May 06 '19 at 14:19
  • 2
    @AdilMalik Because this is a static event.When an event is subscribed, the class that owns the event maintains a reference to the subscriber. As long as this reference exists the subscriber can not be collected by the garbage collector and a memory leak will occur, . – ramaral May 06 '19 at 17:57
  • 1
    @AdilMalik The event should be unsubscribe when no longer needed or just before the subscriber is not need anymore. See [SystemEvents.PowerModeChanged Event](https://learn.microsoft.com/en-us/dotnet/api/microsoft.win32.systemevents.powermodechanged?redirectedfrom=MSDN&view=netframework-4.8) – ramaral May 06 '19 at 18:04
10

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.

Moo-Juice
  • 38,257
  • 10
  • 78
  • 128