Share via


The Power-Aware Application

This topic discusses the programming interfaces that are available for power management. With awareness of the power status, you can respond appropriately to conserve power and meet the user's need to balance power and performance.

Follow these guidelines:

  • Register for and monitor power management events.
  • Avoid delaying inactivity time-outs that lead to sleep.

Scale back activity to limit power usage when the system is in a power conservation mode (discussed in the topic Reducing Power Consumption).

In this topic, the term "sleep" refers to any of the S3, S4, or Fast S4 power states. Windows Vista uses the same notifications for each power state.

Power management interfaces

Windows Vista uses the WM_PO window message to notify applications about power management events. The message includes a set of power broadcast codes that identify the various events. You can subscribe to these events by using two new functions that are introduced in Windows Vista:

RegisterPowerSettingNotificationUnregisterPowerSettingNotification

Power management notifications provide complete information about the power management status of the system and its components. By using notifications, your application eliminates the need to poll the system, which drains energy.

Download the hands-on lab entitled "Building Energy Smart Windows Vista Applications" from the Mobile PC and Tablet PC Hands-On Labs. The C# projects in the lab provide a Powerform.Imports.cs file that contains the definitions that you need to access these functions from managed code.

Monitoring power management events

Call RegisterPowerSettingNotification to request notification of the following power management events. These events are sent with the WM_POWERBROADCAST message under the PBT_POWERSETTINGCHANGE notification.

Notification Occurs when

GUID_ACDC_POWER_SOURCE

The power source changes from DC to AC/DC power, or to a short-term DC source, such as a UPS device. The notification indicates which of these three power sources is active.

Recommendation: Conserve power when the mobile PC is drawing power from a DC source.

GUID_MONITOR_POWER_ON

The display is turned on or off, either by the user or by the system's screen-blanking function. The notification indicates whether the display is on or off.

Recommendation: Don't waste CPU cycles on rendering graphics when the display is turned off.

GUID_BATTERY_PERCENTAGE_REMAINING

The battery charge decreases or increases. The notification indicates the new percentage value for the charge level.

Recommendation: When the battery charge drops below 10%, save data to temporary files.

GUID_POWERSCHEME_PERSONALITY

The user switches to a power plan with a different personality. The notification indicates the new personality type (Balanced, Power saver, or High performance).

Recommendation: Change the power-saving behavior of your application to match the intent of the power plan.

 

The GUIDs that are listed above are defined in the Powerform.Imports.cs file in the hands-on lab. Use the GUID to register for each notification class and to identify the incoming notification that's sent with the WM_POWERBROADCAST message. To learn how to process these new notifications, see the samples in the hands-on labs.

Handling sleep transitions

In Windows Vista, a computer can go to sleep as a result of two types of events:

  • User actions, such as closing the lid or pressing the Power button
  • Idle-to-sleep (an idle period has passed, so the computer goes to sleep)

Smooth transitions to and from sleep are integral to the Windows Vista experience. The PBT_APMSUSPEND suspend notice is broadcast about two seconds prior to the sleep transition. The notice interval is shorter in Windows Vista than in earlier versions of Windows. Also, the standard configuration of Windows Vista prohibits applications from vetoing a sleep transition, although this behavior can be overridden through Group Policy.

Sleeping

Avoid displaying notifications or modal UI when your application is notified of a sleep transition. The user might not see them until the mobile PC resumes from sleep. Additionally, consider the following guidelines:

  • Close and release devices.
  • Pause ongoing processes, such as video or audio playback.
  • Save data to temporary files.

Resuming

Users want and expect to resume work as quickly and seamlessly as possible. So, avoid putting up impediments-such as dialog boxes or notifications-that prevent a user from resuming work right away.

Also, the computer configuration may have changed while the computer was sleeping. For example, consider the following possibilities:

  • The hardware configuration of the computer may have changed during the sleep state.
  • The computer may have been moved to a different location that has a different networking environment.
  • The power source may have changed.

Avoid delaying idle-to-sleep

One key user expectation is that the computer will observe idle time-outs. People expect their computers to shut down display and storage devices and go to sleep after requested idle periods.

In certain rare instances, an ongoing process might take precedence over an idle-to-sleep setting. For example, if a media application is burning a CD (which would be ruined if the process were interrupted), the application might temporarily disable the idle-mode time-out, and then restore the time-out after it finishes burning the CD.

You can use the SetThreadExecutionState function to disable the idle-to-sleep and display-blanking time-outs. However, disable these time-out periods only if absolutely necessary, such as when data or media loss will occur, or when there are special "hands-off" scenarios, such as watching a movie. Be sure to restore the time-out periods after your operation is completed.

To see definitions for the SetThreadExecutionState function, see the hands-on lab entitled "Building Energy Smart Windows Vista Applications" in the Mo.

Checking the power state of system devices

Power plans in Windows Vista include idle period time-outs for various system devices, such as the display and the hard disk. Consider delaying the use of devices that are turned off when the computer is saving power. To check the power state of a storage device, use the Win32 function GetDevicePowerState.

For information about using GetDevicePowerState and GetSystemPowerStatus in managed code, see Mobile PC Power Management on MSDN.

 

 

Send comments about this topic to Microsoft

Build date: 2/8/2011