A power management event is a change in either the system power status or the operational mode of a device or the system. Because these events can affect the operation of applications and installable drivers and can lead to loss of data, the system notifies all applications and installable drivers by broadcasting a WM_POWERBROADCAST message for each event.
A system power status event occurs when there is a change in the power supply or in the system battery status. For example, the system broadcasts a PBT_APMPOWERSTATUSCHANGE message whenever the user switches from battery to AC power or vice versa. The system also broadcasts this message when remaining battery power slips below the threshold specified by the user.
An operational mode event occurs when there is a change in power consumption, such as the system shutting itself down or the user turning the system off. If applications and drivers do not prepare for changes in operational mode, they can lose data. Therefore, the system broadcasts messages about these changes before they actually occur. For example, if the system determines that it is idle, it broadcasts a PBT_APMSUSPEND message notifying applications and drivers that it is about to suspend operation and shut itself down to save power. The applications and drivers can prepare for this suspension by closing files and saving data that may otherwise be lost when power is shut off.
The system has two ways to shut itself down. It can suspend operation or carry out critical suspension. An application can also shut the system down by using the SetSystemPowerState function to suspend operation.
When system operation is suspended, power to all devices is lost. The WM_POWERBROADCAST message, sent immediately before suspension, allows device drivers to save the state of the device. For many devices (but not all), saving the state means the driver can restore the state when power is eventually restored, allowing the device to continue operation as if power had not been lost.
When the system carries out a critical suspension, the system is shut down immediately because battery power is too low to let the system continue to run. In contrast to suspended operation, the system does not notify applications and drivers before carrying out a critical suspension. This means data may be lost.
When system operation is restored after having been suspended, the system notifies all applications and drivers. It also identifies the manner in which the system was previously shut down so the application or driver can take appropriate steps to restore its data and continue its own operation.
Applications should make every attempt to handle the transition to the sleeping state without any user intervention. When the system requests permission to enter the sleeping state, your application should check to see if it has any open files on removable drives or on the network. If so, the application should autosave and close its files. Applications should deny permission to enter the sleeping state only if an uninterruptible operation is in progress.