Power Events


The Windows Service can react when the power status changes. One example of a power event is when the system hibernates - all the memory content is written to the disk, so a faster boot is possible. It’s also possible to suspend the system in order to reduce the power consumption, but it can be awakened automatically on demand.

For all power events, the service can receive the control code SERVICE_CONTROL_POWEREVENT with additional parameters. The reason for the event is passed through these parameters. The reason could be low battery power or the system is going to a suspended state or a power status change. Depending on the circumstances, the service should slow down, suspend background threads, close network connections, close files, and so on.

The classes in the System.ServiceProcess namespace have support for power events. In the same way that you can configure a service so that it reacts to pause and continue events with the CanPauseAndContinue property, you can also set a property for power management: CanHandlePowerEvent. Windows Services that handle power events are registered in the SCM with the Win32 API method RegisterServiceCtrlHandlerEx().

If you set the property CanHandlePowerEvent to true, the method OnPowerEvent() of the class ServiceBase is called. You can override this method to receive power events and to react with your service implementation accordingly. The reason for the power event is passed in an argument of type PowerBroadcastStatus. The possible values of this enumeration are listed in the following table.

Open table as spreadsheet

Value

Description

BatteryLow

The battery power is low. You should reduce the functionality of the service to a minimum.

PowerStatusChange

A switch from battery power to A/C happened, or the battery power slips below a threshold, and so on.

QuerySuspend

The system requests permissions to go into a suspended mode. You could deny the permissions, or prepare to go into the suspended mode by closing files, disconnecting network connections, and so on.

QuerySuspendFailed

The change into the suspended mode was denied for the system. You can go on with the functionality as before.

Suspend

Nobody denied the request to go into the suspended mode. The system will be suspended soon.




Professional C# 2005 with .NET 3.0
Professional C# 2005 with .NET 3.0
ISBN: 470124725
EAN: N/A
Year: 2007
Pages: 427

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net