
.NET 2.0+Creating a Battery Power Status Monitor (2)
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.
The RefreshStatus Method
The controls on the form will be updated in two different places. When the form is loaded for the first time, the controls will be populated. This will ensure that the controls are never shown with default or empty values that could be misconstrued by a user. Additionally, the timer's Tick event will cause a refresh of the information every second.
To centralise the refreshing, a single method will be created containing the update code. Add the following method declaration to the form's class:
private void RefreshStatus()
{
}
Mains Power Display
The first control to be populated is the checkbox that indicates whether the computer is connected to mains power or is running on a battery. This information will be extracted from the PowerLineStatus property. This property returns a value of the PowerLineStatus enumerated type. The value will be "Online" for mains power, "Offline" for battery power or "Unknown" if the status cannot be determined. A switch statement will be used to react to the three possibilities.
To show the power line status, add the following code to the RefreshStatus method. Note that the first line creates a local variable to contain a reference to the PowerStatus property of the SystemInformation class.
PowerStatus power = SystemInformation.PowerStatus;
switch (power.PowerLineStatus)
{
case PowerLineStatus.Online:
MainsPower.Checked = true;
break;
case PowerLineStatus.Offline:
MainsPower.Checked = false;
break;
case PowerLineStatus.Unknown:
MainsPower.CheckState = CheckState.Indeterminate;
break;
}
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;
else
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);
else
BatteryLifeRemaining.Text = string.Empty;
28 April 2008