Windows Vista improves how your application can interact with Windows’ power management. The WM_POWERBROADCAST message is still sent to notify the application of changes. This does mean that there needs to be a Window handle to receive these messages.
" | Windows Vista improves how you can interact with the power management APIs. | " |
Windows Vista then provides a number of GUIDs that you can use to register to receive information about particular features of the power. For example, if you want your application to be notified of the power source, register for the GUID_ACDC_POWER_SOURCE GUID.
The registration method is called RegisterPowerSettingNotification and takes a GUID parameter that identifies the notification for which you are registering.
hPowerSrc =
RegisterPowerSettingNotification(this.Handle,
ref GUID_ACDC_POWER_SOURCE,
DEVICE_NOTIFY_WINDOW_HANDLE);
The returned handle should then be used to unregister the notification.
UnregisterPowerSettingNotification(hPowerSrc);
When the notification messages are sent, they are accompanied by a structure that is passed in the lParam of the Windows message. This structure is a POWERBROADCAST_SETTING, defined here in C#.
[StructLayout(LayoutKind.Sequential, Pack = 4)]
internal struct POWERBROADCAST_SETTING
{
public Guid PowerSetting;
public Int32 DataLength;
}
The PowerSetting field in the structure identifies the setting or event notification you are receiving. Remember, you can register to be notified of changes to multiple power settings.
In Windows Vista, it is easy for the user to change the active Power Plan. A Power Plan specifies the power management settings in effect on the computer. The three power plans that will ship with Windows Vista are Automatic, High Performance, and Power Saver.
As the names suggest, these power plans indicate to the running application how to behave regarding power consumption. In Automatic mode, it is best practice to scale functionality based on criteria, such as application demand, current power source and battery percentage remaining. In High Performance, your application should not scale back functionality to improve battery life; the user has chosen not to worry about battery life and your application should focus on providing the best possible performance. When the user chooses Power Saver, your application should scale back all non-critical functionality to reduce power consumption.
Your application can register for a change in the Power Plan by calling the RegisterPowerSettingNotification method using the GUID_POWERSCHEME_PERSONALITY GUID.
Conclusion
As you can see, it’s wise to add power management to your application. Doing this is not hard and can help to improve the user experience by extending the battery life of the device on which your application is run.
To prevent the user experiencing a drained battery, your application should always respond in a timely manner to Suspend requests and never block the computer from suspending.
Here is a list of some of the things you should consider when building your application:
- Determine the power source.
- Never prevent a request for the system to suspend.
- When the system resumes, your application should run smoothly, as if it had never been interrupted.
- When the system resumes, the available devices and power source may have changed. Your application should be reasonably unaffected by these changes or have built-in contingency plans.
- Reduce the power consumption of your application when the power source is a battery by:
- Avoiding polling operations
- Not running non-critical worker threads
- Avoiding unnecessary device (disk, display, Bluetooth, USB) access