Click here to Skip to main content
15,881,281 members
Articles / Programming Languages / C#

PowerMode, change and status in .NET

Rate me:
Please Sign up or sign in to vote.
4.46/5 (14 votes)
13 Sep 2004CPOL2 min read 60.9K   587   29   6
Making your application Power Aware, in .NET.

Introduction

This articles explains how to make an application aware of the Battery Status when in use on a laptop/tablet.

PowerMode change comes from Intel documentation on devx. The current power status comes from MSDN in a longhorn article.

Background

See Cancellable Thread Pool for why this is here. This is a very raw and ready post as it's just a couple of searches anyway. The only bit of my code really is the constructor which ensures you have results whenever you instantiate the class.

Using the code

Run the application, and then attempt to generate as many PowerChange events as possible. With a modern ACPI capable desktop PC, you will probably be able to generate Suspend and Resume events. Attempting to change the ACLineStatus is not recommended.

With a laptop/tablet, you should be able to generate the full range of events as long as your battery holds some charge.

The MSDN/Intel documentation does not indicate if these power events are also available from servers via UPS. If anyone has a development machine on a UPS, I would be interested to see a trace of the events. (The file is automatically saved when the application shuts down, see frmPowerDemo_Closed().)

Code Features

There are two parts to this code, firstly detecting PowerChange events, and secondly, finding out the current status. To detect a PowerChange, you first need to use the Microsoft.Win32 namespace, and wire in the event.

C#
//At the Top of the file 
using Microsoft.Win32;
    
//In the Constructor or elsewhere
    SystemEvents.PowerModeChanged 
        += new PowerModeChangedEventHandler(SystemEvents_PowerModeChanged);

Next, it's a simple case of declaring your handler in the usual method.

C#
private void SystemEvents_PowerModeChanged(object sender, 
                          PowerModeChangedEventArgs e)
{

The PowerModeChanged event provides the initial information as to why you get the event, e.g., PowerModeChangedEvent.Suspend, .Resume and .StatusChange. However, you will usually require slightly more complex logic, e.g.:

C#
//Get the current Status
PowerStatus ps = new PowerStatus();
switch (e.Mode)
{
case PowerModes.Resume:
    if (ps.BatteryFlag & _BatteryStatus.Critical
        != _BatteryStatus.Critical)
    {
        //Start process that will use lots of CPU

The PowerStatus class is used to provide detail to the current state. It will automatically read the current power status when it is constructed, so all you need to do is read the values off. Of most value are the BatteryFlag and ACLineStatus fields. Both of these are enumerated to provide useful information, although the BatteryFlag is a bitmapped field.

Points of Interest

This code can be used in combination with Threading to provide applications that will throttle back the CPU usage as power becomes critical.

History

v0.1a 13th Sept 2004. First release.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
** Apologies but my daughter was born in October 2004, and so coding now comes second. My reponses tend to take a lot longer**

I've been coding since I got my first ZX Spectrum. From Basic to assembly, through C,C++ and arriving at C#. On the way I've throughly enjoyed Perl, Lisp and XML.

I find I can make the intellectual leap to understand the problem, I love big picture designs, patterns and reuse. I may be addicted to abstract classes Smile | :) GOF has a lot to answer for. I miss delete() even though I spent too much time finding the leaks.

My favourite part of coding is in UI design because of the complexity, the event driven nature, and the fact its (virtually) tactile. I hate GUI's that don't follow system guidelines, don't resize, and don't display properly when you change system colour and font.

Comments and Discussions

 
QuestionWork perfectly still Pin
Charlie Shin27-Jan-20 20:34
Charlie Shin27-Jan-20 20:34 
GeneralIn my program it is not working, but in a fresh new project it works... Pin
samprog1-Jul-09 0:50
samprog1-Jul-09 0:50 
GeneralTest power of Destop PC Pin
phuong oanh7-Apr-08 20:34
phuong oanh7-Apr-08 20:34 
Generalfm Pin
vinothraj4-Dec-07 19:06
vinothraj4-Dec-07 19:06 
GeneralExcellent work. Pin
Dr.Luiji16-Feb-07 8:46
professionalDr.Luiji16-Feb-07 8:46 
GeneralThank You Pin
Agent 8616-Jun-05 7:03
Agent 8616-Jun-05 7:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.