SIGN UP MEMBER LOGIN:    
ARTICLE

Power Management from a Win Forms Application

Posted by Scott Lysle Articles | Windows Forms C# January 17, 2007
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.
Reader Level:
Download Files:
 

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 last left 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 win 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.

Login to add your contents and source code to this article
share this article :
post comment
 

I have project related to power management in networks and will appreciate if you could provide more information about source where I can get more information regarding this project

Posted by riyaz ahmed Nov 09, 2007
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor