Click here to Skip to main content
13,351,731 members (41,736 online)
Click here to Skip to main content
Add your own
alternative version


20 bookmarked
Posted 6 May 2011

Responding to Windows power management events (standby, hibernate, shutdown)

, 6 May 2011
Rate this:
Please Sign up or sign in to vote.
Making your application exit gracefully by handling Windows power management events (standby, hibernate, shutdown)


This article provides a way for a .NET Windows application to respond to Windows Power Management events such as suspend, hibernate, standby, power switch, etc.


My organization uses Lotus Notes as an email client which hangs badly on our laptops when we go from one meeting room to another since the network breaks between the meeting rooms. And since Lotus Notes continuously monitors the network for any incoming mail, it tends to get hung up taking the whole window down with it. So to avoid forcefully restarting Windows, I created this application to capture the standby event and close the mail client before going to standby and automatically start the mail client when the system resumes. I got a lot of reference help from the following article:

Using the Code

We need to override the WndProc method which will allow us to interact with all the events that the OS tends to provide to the application on different conditions.

The Windows OS communicates with the application by sending different messages which need to be captured and worked on. For this implementation, we need to capture the WM_POWERBROADCAST (0x0218) value which will specify that this is a power event change and that we need to capture the WParam which can have the following values:

  • PBT_APMSUSPEND (0x0004)
  • PBT_APMSTANDBY (0x0005)

These values will help us to manage the application data when a power management event occurs. More details of the messages passed to the application are available at this link.

/// <summary>
/// Overloaded System Windows Handler.
/// </summary>
/// <param name="m">Message <see cref="Message"/> structure</param>
protected override void WndProc(ref Message m)
    switch (m.Msg)
            switch (m.WParam.ToInt32())
                //value passed when system is going on standby / suspended
                case PBT_APMQUERYSUSPEND:
                    List<string> arr = 
                    foreach (string item in arr)
                        foreach (Process p in 
                                //Exiting the applications when going into standby
                                p.WaitForExit(3600000); // 1 min timeout
                                // process was terminating or can't be terminated
                //value passed when system is resumed after suspension.
                case PBT_APMRESUMESUSPEND:
                    arr = Properties.Settings.Default.StandUpApps.Split(',').ToList();
                    foreach (string item in arr)
                        //Starting the applications when coming up from standby

                //value passed when system Suspend Failed
                case (PBT_APMQUERYSUSPENDFAILED):
                //value passed when system is suspended
                case (PBT_APMSUSPEND):
                //value passed when system is resumed automatically
                case (PBT_APMRESUMEAUTOMATIC):
                //value passed when system is resumed from critical
                //suspension possibly due to battery failure
                case (PBT_APMRESUMECRITICAL):
                //value passed when system is low on battery
                case (PBT_APMBATTERYLOW):
                //value passed when system power status changed
                //from battery to AC power or vice-a-versa
                case (PBT_APMPOWERSTATUSCHANGE):
                //value passed when OEM Event is fired. Not sure what that is??
                case (PBT_APMOEMEVENT):
    base.WndProc(ref m);

Points of Interest

The main learning point was how to capture the events for standby, suspend, and resume of Windows which will be useful to gracefully handle these kinds of events to prevent data from getting corrupted.


  • Version 1.0 - Initial draft (5th May 2011)


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Maharishi Bhatia
Program Manager Capgemini
United States United States
I have been working in software industry for the past 11 years, though i am trained to be a pharmacist. Enjoy working on complex and new technologies. Also cleared my SCJP and MCP certification examinations.

You may also be interested in...


Comments and Discussions

GeneralMy vote of 4 Pin
klinkenbecker30-Nov-11 12:42
memberklinkenbecker30-Nov-11 12:42 
GeneralRe: My vote of 4 Pin
maharishi_b23-Feb-12 7:42
membermaharishi_b23-Feb-12 7:42 
Questionresume event Pin
Rahul Nalavade23-Aug-11 9:14
memberRahul Nalavade23-Aug-11 9:14 
GeneralMy vote of 1 Pin
Cristian Amarie8-May-11 3:12
memberCristian Amarie8-May-11 3:12 
GeneralMy vote of 1 Pin
Cristian Amarie7-May-11 1:18
memberCristian Amarie7-May-11 1:18 
GeneralRe: My vote of 1 Pin
Chris Maunder7-May-11 2:02
adminChris Maunder7-May-11 2:02 
GeneralRe: My vote of 1 Pin
Cristian Amarie7-May-11 3:16
memberCristian Amarie7-May-11 3:16 
GeneralRe: My vote of 1 Pin
Chris Maunder7-May-11 7:53
adminChris Maunder7-May-11 7:53 
GeneralRe: My vote of 1 Pin
S Mewara7-May-11 20:07
mvpS Mewara7-May-11 20:07 
You say 'My vote of 1' but article has 2 5's... you just made a comment or your vote is missing? Unsure | :~

GeneralRe: My vote of 1 Pin
Cristian Amarie8-May-11 3:11
memberCristian Amarie8-May-11 3:11 
GeneralRe: My vote of 1 Pin
S Mewara8-May-11 3:20
mvpS Mewara8-May-11 3:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180111.1 | Last Updated 6 May 2011
Article Copyright 2011 by Maharishi Bhatia
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid