views:

410

answers:

4

Is it possible to subscribe to a Windows event that fires when Windows is going into or coming out of Sleep or Hibernate state?

I need my application to be made aware when the computer is going to sleep to do some cleanup and avoid timing issues when it comes out of sleep.

+2  A: 

You can monitor Win32_PowerManagementEvent WMI event

aku
+2  A: 

In .NET, use the PowerModeChanged event. In Win32, use the WM_POWERBROADCAST message.

Hugh Allen
+5  A: 

Microsoft.Win32.SystemEvents.PowerModeChanged event will give you this information. This event is available in all variants of the .NET framework released by Microsoft so far.

Jeff Yates
A: 

Not sure how often you want to monitor this, but if you write a service in .NET you can override ServiceBase, set CanHandlePowerEvent to true, and then you'll be notified of power changes via the PowerBroadcastStatus enumeration.

grettke