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 2.0+

Creating a Battery Power Status Monitor

Sometimes programs need to know the power status of a computer. An example is Windows Update, which often requires a computer to be running on mains power before updates are installed. This article explains how to read the power status and battery life.

Battery Remaining Percentage

The next control to be updated is the progress bar that gives a graphical display of the percentage of battery power remaining. This progress bar will be populated with a value related to that of the BatteryLifePercent property. However, as this property returns a value between zero and one for valid charge percentages, the value will be multiplied by one hundred and cast as an integer.

A check is made before applying the percentage to the progress bar's value. This ensures that the percentage is less than or equal to one hundred. A larger value, specifically 255, indicates that the battery charge information is not available.

Add the following code to the bottom of the RefreshStatus method:

int powerPercent = (int)(power.BatteryLifePercent * 100);
if (powerPercent <= 100)
    BatteryIndicator.Value = powerPercent;
    BatteryIndicator.Value = 0;

Battery Time Remaining

The BatteryLifeRemaining property returns the estimated number of seconds before the battery will be exhausted. However, this estimate is not updated constantly by Microsoft Windows. This means that when the mains power is first removed, the property holds -1 to indicate that the time is unavailable. Also, if the time remaining is displayed in seconds, this value will not update constantly and gives the impression of being inaccurate.

To avoid these aesthetic problems, we will check that a positive number of seconds has been returned before showing the value. We will also divide the value by sixty and express the time remaining in minutes. To add this functionality, add the following code to the end of the RefreshStatus method:

int secondsRemaining = power.BatteryLifeRemaining;
if (secondsRemaining >= 0)
    BatteryLifeRemaining.Text = string.Format("{0} min", secondsRemaining / 60);
    BatteryLifeRemaining.Text = string.Empty;

Showing the Battery Status

The BatteryChargeStatus property returns a value from the BatteryChargeStatus enumeration. This bit field enumeration has six possible values that sometimes can be combined. The values are:

Enumeration ConstantMeaning
ChargingThe battery is currently being charged.
CriticalThe battery level is below the critical level specified in the user's preferences.
HighThe battery level is high.
LowThe battery level is low.
NoSystemBatteryThe computer does not have a battery.
UnknownThe battery status could not be determined.

As the values can be combined automatically, we will simply convert the property value to a string and assign it to the label's Text property. To do so, add the following code to the foot of the RefreshStatus method:

BatteryStatus.Text = power.BatteryChargeStatus.ToString();

Adding the Form Load Event

On loading the form, two activities must occur. Firstly, the form's controls will be updated to show the current battery and power information. Secondly, the timer will be enabled so that the information is refreshed periodically. Add the form load event to the code and modify it as follows to add these two key activities:

private void BatteryMonitorForm_Load(object sender, EventArgs e)
    RefreshTimer.Enabled = true;

Adding the Timer Event

Finally, the constant updating of power information can be added by calling the RefreshStatus method from within the timer's Tick event. Add this event and modify it as follows:

private void RefreshTimer_Tick(object sender, EventArgs e)

You can now execute the program to see the results. If you are using a notebook computer, try removing and re-inserting the power cable to see the refreshing in action.

28 April 2008