Tuesday, March 27, 2012

Handling Windows power state

- Rameshkumar Subramanian
While developing CloudStorage Drive [virtual drive] application I came across exploring windows power management to remount the CloudStorage Drive after getting up from hibernate state. Usually you can see our local drive / regular drive, like C:\, gets mounted when the system wakes up from sleep state.

We know that the operating system controls power management across the entire PC system. For example, suppose if you have paused a video and go to the hibernate state the process goes idle here. You can see the video remain on the same state when the system is on back.

The software developers can now make their application power-aware by handling the WM_POWERBROADCAST message provided by the windows operating system and scaling features accordingly. This article describes how to handle the WM_POWERBROADCAST message and also describes the sequence of messages for four typical cases. This is very helpful for whoever wants to re-launch the application after the system get back to power state.

Power States
To the user, the PC is either on or off, and other conditions are not visible. However, the ACPI (Advanced Configuration and Power Interface) specification defines a number of different power states that are supported by windows operating system.

State
Description
S0/Working
On. The CPU is fully up and running. power conversation is on a per-device basis.
S1 Sleep
Appears off. The CPU is stopped. RAM is refreshed, the system is running in a low power mode.
S2 Sleep
Appears off. The CPU has no power. RAM is refreshed. The system is in a lower mode than S1.
S3 Sleep
Appears off. The CPU has no power. RAM is in slow refresh. The power supply is in a reduced power mode.
S4 Hibernate
Appears off. The hardware is completely off. But system memory has been saved to disk.
S5/Off
Off. The hardware is completely off. The operating system has shut down, nothing has been saved. Requires a complete reboot to return to the working state.

The power management functions and messages retrieve the system power status and notify applications of power management events. The windows offer WM_POWERBROADCAST message which will be send to the application to indicate power changes. The documentation for each of these is provided as:

Event
Meaning
PBT_APMBATTERY LOW
Battery power is low.
PBT_APMOEMEVENT
OEM-defined event occurred.
PBT_APMPOWERSTATUSCHANGE
Power status has changed.
PBT_APMQUERYSUSPEND
Request for permission to suspend.
PBT_APMQUERYSUSPENDFAILED
Suspension request denied.
PBT_APMRESUMEAUTOMATIC
Operation resuming automatically after event.
PBT_APMRESUMECRITICAL
Operation resuming after critical suspension.
PBT_APMRESUMESUSPEND
Operation resuming after suspension.
PBT_APMSUSPEND
System is suspending operation.


The window proc receives the entire event which we can easily implement as shown below. In .Net you have to using interop service namespace for those things .

protected override void WndProc(ref Message m)
{
    base.WndProc(ref m);
    switch (m.Msg)
    {
    case WM_POWERBROADCAST:
    {
     switch (m.WParam.ToInt32())
      {
         case (int)PBT.PBT_APMQUERYSUSPEND:
          . . .
          . . .
      }
     }
    }
}
The above code can be made use to handle the power suspend states like when system goes sleep or to hibernate mode.

4 comments:

Offshore Software Development Services said...

Interesting to read that post. You are really understand how to catch the viewers of
.Net Development Company.

data recovery services cost said...

Huge piece of writing. I simply came across your medium and want to tell you that I’ve certainly enjoyed browsing your blog posts.

Richard Majece said...

Do you want to learn more about writing narrative essay? I am sure that this information will be useful for college students

Scott Stayris said...

Quickbooks is the advanced accounting software to maintain records of business data. The software is enriched with multiple premium features to enhance your business. How to backup Quickbooks Online?