Power Events C# Help

The Widows Service can react when the power status changes. One example of a power event is when  he system hibernates – all the memory content is written to the disk, so a faster boot is possible. It is also possible to suspend the system 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_POWEREVENTwith additional parameters. The reason for the event is pushed through these parameters. Th,e reason could be low battery power, it power status change, or the system is going to a suspended state. Depending on the circumstances, the service should slow down, suspend background threads, close network connections, close files, and so on.

r(gure 23-22

Figure 23-22

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.

Untitled

Posted on October 31, 2015 in Windows Services

Share the Story

Back to Top