Power Management From a Windows Forms Application

Introduction

This article describes how to place the computer into a power management suspend state, and how to put the computer into a power management related hibernate state.

The suspend option places the computer into a low power consumption mode of operation (without saving any current state information) while the hibernate option saves the current state prior to entering into the low power mode of operation. As a result of the differences between suspend and hibernate, the user may restore from suspend quicker than from hibernate but any open applications will not survive the trip. Restoring from hibernate is slower than suspend but all open applications will be restored to the previous configuration.

You might find the examples useful if you need to forcibly manage power state on mobile computers following some period of inactivity. It does not take a whole lot of imagination to figure out that one could build some interesting joke applications around these methods as well.



Figure 1.  Demonstration Form in Sample Application

Getting Started.

The solution provided with this download contains a single project entitled, "Shutdown". The project is described in the Solution Explorer (Figure 2). The project contains only the default references for a Windows Forms application. All of the code written for this project is included in frmMain.



Figure 2.  Solution Explorer
 

The example was written using Visual Studio 2005 using C#; open the solution into the IDE to examine the code. 

The Code:  Main Form.

There is only a single form in the application (frmMain) and that form contains all of the code necessary to perform all of the functions described in the introduction: Suspend, and Hibernate. The class begins with the normal defaults.

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

 

namespace Shutdown

{

    public partial class frmMain : Form

    {

        public frmMain()

        {

            InitializeComponent();

        }
            ...
 

The next bit of code in the application is the exit button's click event handler, this code terminates the application by calling the Exit method from the Application class.

private void btnExit_Click(object sender, EventArgs e)

{

    Application.Exit();

} 

 

Following the exit button's click event handler, the next bit of code is used to place the machine into the suspended power state. That code is as follows:

 

// Power Management Related Options 

// Suspend

private void btnSuspend_Click(object sender, EventArgs e)

{

    Application.SetSuspendState(PowerState.Suspend, false, false);

} 

 

The SetSuspendState method of the application class accepts three arguments, the first is the power state from the power state enumeration (which contains two options: suspend and hibernate), the next argument is a Boolean value used to instruct the method as to whether or not it should force the transition to the suspended power state, and the last argument is used to instruct the method as to whether or not the wake event should be disabled.

The next click event handler demonstrates the approach used to place the machine into hibernation: 

// Hibernate

private void btnHibernate_Click(object sender, EventArgs e)

{

    Application.SetSuspendState(PowerState.Hibernate, false, false);

} 

 

The arguments are the same as were described for the last example with the exception of setting the power state to hibernate instead of suspend.

That is all that is required to set the user's machine to the suspend or hibernate power saving modes of operation. 

Summary

The example provided shows a couple of approaches to managing the power state of the user's machine by demonstrating how to set the machine's power state to suspend or hibernate. The demonstration application has no value but the approach could be used to do a few useful things such as to evoke a power saving mode of operation after some period of inactivity.


Similar Articles