BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

System Information
.NET 1.1+

Detecting Power Events

Microsoft Windows raises notifications in response to power events, including when the user sleeps, hibernates or resumes the computer, or when the power supply switches between battery mode and A/C power. The notifications can be detected using events.

Power Events

Some software must react to changes in the power status of the computer. To enable you to subscribe to such notifications, the .NET framework includes a class that raises events when power status changes occur. The event, named PowerModeChanged, includes event arguments held in a PowerModeChangedEventArgs object. These event arguments include the Mode property, which holds a PowerModes enumeration constant describing the type of change. This can be one of three possible values:

  • Suspend. Indicates that the computer was suspended. This happens when the machine enters sleep mode or is hibernated.
  • Resume. Indicates that the computer has been resumed following sleeping or hibernating.
  • StatusChange. Indicates that another type of power status change has occurred. This power mode is indicated when the battery enters a critical state or when switching between A/C power and battery power.

Subscribing to the PowerModeChanged Event

We'll create a very simple console application that subscribes to the PowerModeChanged event and displays the mode when the status changes. The types required for this example are defined in the Microsoft.Win32 namespace. To simplify access to those members, add the following using directive to any code that uses the types.

using Microsoft.Win32;

The event is defined in the SystemEvents class as a static member. Subscribe to the event using the code shown below. In the Main method we register a method to be called when the event is raised and then wait for Enter to be pressed using Console.ReadLine. When the ReadLine is reached, the program does nothing except react to the power event being raised. When the event is detected, the SystemEvents_PowerModeChanged method is executed. This outputs a message to the console showing the type of power status event captured.

static void Main(string[] args)
{
    SystemEvents.PowerModeChanged +=
        new PowerModeChangedEventHandler(SystemEvents_PowerModeChanged);
    Console.ReadLine();
}

static void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)
{
    Console.WriteLine(e.Mode);
}

If you are using a computer that has a battery, run the program and try plugging and unplugging the power lead. Each time you switch between A/C power and battery you should see the message below. You should also see the message if you allow the battery to discharge to a critically low level. The event does not allow you to differentiate between these occurrences. However, you can check the battery level and find out if the computer is running under battery power using the techniques described in the article, "Creating a Battery Power Status Monitor".

StatusChange

Whilst the program is still executing, try suspending the computer. This will cause the event to be raised with a PowerMode of Suspend. This message will be hidden as the machine powers down. When you resume, the event will be captured for a second time, causing a second message to be written to the console. The output from the two events should be as follows:

Suspend
Resume

Detaching Events

The PowerModeChanged event is a static event. If you do not detach from static events they retain references to your objects even after they are no longer in use. This prevents the garbage collector from releasing objects and can lead to memory leaks. Although in our simple program this is not really a problem, we can correct the potential memory link by correctly detaching, as follows:

SystemEvents.PowerModeChanged -=
    new PowerModeChangedEventHandler(SystemEvents_PowerModeChanged);
6 May 2012