Application Power Management Best Practices for Windows Vista - 31

Application Power Management Best Practices for Windows Vista

November 20, 2007

Abstract

The Windows Vista® operating system features significant changes to power management infrastructure, functionality, and user experiences. These changes impact all Microsoft® Windows® components, including third-party applications and services. Application developers must be aware of the changes to power management in Windows Vista, and design and test their applications accordingly.

This paper details the power management best practices for Windows Vista applications, including correctly handling sleep and resume transitions, responding to common system power events and designing for entertainment and media PC scenarios.

This information applies for the following operating systems:
Windows Server® 2008
Windows Vista

The current version of this paper is maintained on the Web at:
http://www.microsoft.com/whdc/system/pnppwr/powermgmt/PM_apps.mspx

References and resources discussed here are listed at the end of this paper.


Disclaimer

This is a preliminary document and may be changed substantially prior to final commercial release of the software described herein.

The information contained in this document represents the current view of Microsoft Corporation on the issues discussed as of the date of publication. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information presented after the date of publication.

This White Paper is for informational purposes only. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS DOCUMENT.

Complying with all applicable copyright laws is the responsibility of the user. Without limiting the rights under copyright, no part of this document may be reproduced, stored in or introduced into a retrieval system, or transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for any purpose, without the express written permission of Microsoft Corporation.

Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property rights covering subject matter in this document. Except as expressly provided in any written license agreement from Microsoft, the furnishing of this document does not give you any license to these patents, trademarks, copyrights, or other intellectual property.

Unless otherwise noted, the example companies, organizations, products, domain names, e-mail addresses, logos, people, places and events depicted herein are fictitious, and no association with any real company, organization, product, domain name, email address, logo, person, place or event is intended or should be inferred.

© 2006-2007 Microsoft Corporation. All rights reserved.

Microsoft, MSDN, PowerPoint, Visual Studio, Windows, Windows Server, and Windows Vista are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries.

The names of actual companies and products mentioned herein may be the trademarks of their respective owners.

Document History

Date / Change /
December 1, 2006 / First publication
November 20, 2007 / Correct location for obtaining Power Event Monitoring tool.


Contents

Introduction 4

Handling Sleep and Resume Transitions 5

Sleep, Stand By, Hibernate, and Hybrid Sleep 5

Sleep and Resume Events 5

Changes to Sleep and Resume Events 6

Responding to Sleep Events 6

Responding to Resume Events 7

Handling Network Connections and Files 8

Preventing System Idle Timeouts 8

Display Idle Timeout 9

Sleep Idle Timeout 9

Using SetThreadExecutionState 9

SetThreadExecutionState Notes 11

Designing for Extended Battery Life 11

Best Practices for Extending Battery Life 12

Battery Life Impact of High-Resolution Periodic Timers 12

Responding to Common Power Events 14

Registering for Power Events 14

Responding to Power Events 15

Unregistering for Power Events 16

System Power Source (AC/DC) Notifications 17

Power Plan Personality Notifications 18

Monitor Power State Notifications 19

Battery Capacity Remaining Notifications 19

Background Task Notifications 20

Designing for Entertainment and Media PC Scenarios 21

Responding to Away Mode Notifications 21

Requesting Away Mode 22

Using Power Management APIs from Managed Code 23

Responding to Sleep and Resume Events 23

Responding to Common Power Events 24

Additional Managed Code Samples and Information 26

Designing for Earlier Versions of Windows 26

Responding to Power Events within a Windows Service 27

Testing Applications for Power Management 28

Introduction to the PwrTest Utility 28

Using PwrTest to Automate Sleep and Resume Transitions 28

Using PwrTest to Observe SetThreadExecutionState Calls 29

Introduction to the Power Event Monitoring Tool 29

Call to Action 30

References 31

Introduction

The Windows Vista operating system features significant changes to power management infrastructure, functionality, and user experiences. These changes are designed to enable the following key goals for Windows Vista power management:

·  Energy-saving features that are enabled by default. Windows Vista helps extend mobile PC battery life and reduce desktop and workstation power consumption by enabling energy-saving features by default.

·  Deterministic and reliable sleep transitions. Sleep and resume transitions in Windows Vista are responsive and deterministic. Applications, services, and device drivers cannot block in-progress sleep transitions, which gives users additional confidence when they close the lid of a mobile PC or power off a desktop system.

·  System-wide responsiveness to user power policy. Windows Vista users can easily change the overall energy-savings or performance behavior of their system.

·  Media and entertainment PC scenarios. Windows Vista introduces Away Mode, a power management feature that enables the PC to look and feel off while it remains on to record television content or service network requests for media sharing.

Realizing each of these power management goals requires changes to the Windows® operating system as well as hosted applications and services.

This paper describes the power management changes for Windows Vista and demonstrates power management best practices for applications, including:

·  Handling sleep and resume transitions. Applications must be able to correctly handle sleep and resume transitions. In Windows Vista, applications can no longer block in-progress sleep transitions.

·  Temporarily preventing system idle timeouts. Applications can temporarily prevent the system sleep and display idle timeouts to ensure that background tasks such as device synchronization, hard disk defragmentation, and media recording complete successfully.

·  Responding to common power events. Applications must be able to run on mobile platforms and respond to common system power events, such as power plan personality and system power source (AC/DC) changes, to help extend system battery life.

·  Enabling media and entertainment PC scenarios. Applications might be required to respond to Away Mode transitions and to pause local media playback to help ensure that the PC looks and sounds off while it is in Away Mode.

The Windows ecosystem supports many application types and designs. The power management best practices that this paper demonstrates might not address all applications or scenarios. Application developers should use this paper and additional online resources from Microsoft to ensure that their applications correctly handle power management scenarios.

Handling Sleep and Resume Transitions

All PCs running Windows Vista will experience sleep and resume transitions. Mobile PCs will continue to use the sleep state as a way to extend working battery life. However, desktop PCs running Windows Vista will have a sleep idle timer that is enabled by default so that, after a period of inactivity, the system automatically enters the sleep state.

Sleep transitions in Windows Vista are improved to help ensure responsiveness and deterministic behavior. These improvements prevent applications from blocking an in-progress sleep transition. Developers must design and test applications so that they can handle these changes to sleep and resume transitions in Windows Vista.

Sleep, Stand By, Hibernate, and Hybrid Sleep

Windows uses several terms to identify low-power sleep states. Windows Vista supports the following low-power sleep states:

·  Sleep (suspend-to-RAM). System context is saved to main memory, and the memory is kept in a low-power (self-refresh) mode while the system is asleep.

·  Hibernate (suspend-to-disk). System context is saved on the system hard disk drive.

·  Hybrid sleep (a combination of sleep and hibernate). System context is saved both in memory and on the hard disk drive.

Applications receive the same set of sleep and resume events for each low-power sleep state. They are not required to handle sleep or resume differently for each low-power sleep state and should not attempt to do so.

Sleep and Resume Events

The WM_POWERBROADCAST message notifies Windows applications of a system sleep or resume transition. Applications receive this message when a power management event occurs, including a sleep or resume transition, a power setting change, or a battery status change.

The WM_POWERBROADCAST message includes one of the following event types that indicate the exact power management event that has occurred on the system:

·  PBT_APMSUSPEND
This event signals that the system is suspending. It is delivered each time the system suspends.

·  PBT_APMRESUMEAUTOMATIC
This event signals that the system is resuming. It is delivered on every resume and does not indicate whether a user is present at the system.

·  PBT_APMRESUMESUSPEND
This event signals that the system has resumed and a user is present at the system. It is delivered only if a user presses a button (such as the power or sleep button) or generates input through a mouse or keyboard to indicate his or her presence at the system.

Changes to Sleep and Resume Events

Windows Vista includes two primary changes to sleep and resume events:

·  The PBT_APMQUERYSUSPEND event is no longer delivered to applications. In earlier versions of Windows, the PBT_APMQUERYSUSPEND event allowed applications to block the sleep transition from completing by returning BROADCAST_QUERY_DENY.

Some developers incorrectly use this message to avoid designing and testing for sleep and resume transitions. For example, an application might return BROADCAST_QUERY_DENY and then display user interface (UI) that asks for input. However, if the system is a mobile PC with the lid closed, prompting for user input is ineffective.

Some applications return BROADCAST_QUERY_DENY to help protect access to a remote network resource such as an open file. However, these same applications must correctly handle transient network conditions, which are prevalent in wireless networks.

·  Applications must process the PBT_APMSUSPEND event within 2 seconds. This change is designed to help increase the performance and responsiveness of suspend transitions. If an application has not returned from the PBT_APMSUSPEND event after 2 seconds, any remaining processing completes after the system resumes from the sleep state.

To ensure that applications correctly handle sleep and resume messages, developers should follow the best practices in the following sections of this paper.

Responding to Sleep Events

Windows delivers the PBT_APMSUSPEND event to applications when the system enters the sleep state. Applications have 2 seconds to finish processing the PBT_APMSUSPEND event before Windows continues with the sleep transition.

Application developers should understand the process by which Windows transitions the system to a sleep state:

1. A user or the system sleep idle timer initiates the sleep transition.

2. The Windows kernel power manager notifies applications, services, and device drivers with the appropriate suspend events, including PBT_APMSUSPEND.

3. Windows places the system hardware in a sleep state and saves system context in memory, on disk, or both in memory and on disk.

4. The system resumes from the sleep state later due to the user powering on the system, a timer expiring, or a wake-on-LAN event.

5. Device drivers, services, and applications are notified that the system has resumed with the appropriate events.

User-mode threads, including those threads of applications and services, continue to execute during the entire sleep and resume transition.

The implication of this design is that many applications do not need to do anything specific to correctly handle sleep transitions. The following are examples of applications that are unaffected by sleep and resume events and that might not be required to process these events:

·  A calculator application that relies solely on user input for activity

·  Entertainment applications such as a simple game

·  A photo application that displays pictures in a sideshow format

·  A Web browser that displays a Web page

However, some applications might be required to respond to the PBT_APMSUSPEND event within 2seconds when the system is entering the sleep state. Typically, applications do very little work in response to the PBT_APMSUSPEND message, such as updating the application state to indicate that the system has entered sleep so that a corresponding action can be taken when the system resumes.

The following types of applications might respond to the PBT_APMSUSPEND event:

·  Peer-to-peer or network applications that track user presence might transmit a message that indicates the user is absent.

·  Optical media creation applications might attempt to stop the in-progress media creation activity. The application should attempt to automatically restart the media creation operation when the system resumes or prompt the user that the creation was interrupted.

·  A print or scan application might tell the printer or scanner device to stop the current operation and update its state to automatically retry the operation when the system resumes.

·  An application that requires the system to wake automatically in the future from a timer might program a waitable timer by using the SetWaitableTimer function.

Responding to Resume Events

The PBT_APMRESUMEAUTOMATIC and PBT_APMRESUMESUSPEND events notify applications that the system has resumed. Applications have up to 20 seconds to respond to these resume events.

The PBT_APMRESUMEAUTOMATIC event is delivered to applications on every resume, but the PBT_APMRESUMESUSPEND event is delivered on resume only when a user is present at the system. Windows uses notifications from the platform hardware and user input at the keyboard or mouse to determine that a user is present at the system. Applications receive both the PBT_APMRESUMEAUTOMATIC and PBT_APMRESUMESUSPEND event when the system resumes from sleep and a user is present.

Applications typically use the resume events to automatically start or restart tasks, reconnect to network resources, and update the information that they are presenting to the user.

When handling resume events in applications, developers should ensure that:

·  Applications automatically reconnect to network resources in the background without interrupting the user.