t's the responsibility of the developer to ensure the best user experience for the increasing number of mobile PCs being bought and used. Every activity your application carries out can affect the power consumption of any computer. This article will help you understand the critical role your software can play in increasing battery life for your users.
CPUs have gotten faster, hard disks have gotten bigger and RAM has become more abundant in the computers we use, but one aspect of computing has not moved anywhere near as fast. Battery technology has not seen the same innovations as other areas of technology and still acts as a drawback to working on mobile computers.
This limited improvement in battery life means that, as software developers, we need to think about how we can help our software's users increase the length of time their mobile computers can be used before the battery is drained.
It may seem that there is little you can do to improve the battery life because it's a hardware issue; at first, I felt this way too, but surprisingly, software can have a huge affect on the lifetime of the battery. For example, scaling down functionality can drastically increase the lifetime of the battery in a mobile PC, as can a periodic timer for polling or background work.
Here are some key areas to consider for providing a better user experience to users running your software on mobile PCs: current power source (AC or DC), seamless suspend/resume transitions, use of the screen and graphics processor, use of the hard disk, use of the network, and use of other powered peripherals.
|The devices attached to the computer when it suspends may have changed by the time the computer resumes operations.|
Consider an application that carries out some background tasks and performs regular saves of data to the hard disk. If the background task forces the CPU to step up,
there is an increase in power consumption. Each time the hard disk is used, there is an increase in power consumption. Reducing the number of times the background tasks occur and increasing the time between data saves could help to improve battery life.
The first step to extending battery life is to discover the current power source.
Source of Power
In the Windows XP platform, there is an API function in Kernal32.dll that allows you to discover the current power source. This method is called GetSystemPowerStatus
takes a reference to a SYSTEM_POWER_STATUS
structure. To use this from managed code, it is a good idea to declare the structure as a class, as it will then be passed by reference (see Listing 1
Using the GetSystemPowerStatus
method creates an instance of a SYSTEM_POWER_STATUS
class and passes it as the parameter to the method. When the method returns, you can examine the fields of the class to discover information on the current power source, as shown in Listing 2
(On some devices, the BatteryLifeTime
fields may not be available. For this reason, it is often best to use the percentage of battery life remaining.)
Once you know whether the computer is being powered from the battery or an AC power source, you can make some decisions as to how your application should behave. The big consumers of power on a mobile PC are the display, disk drives, and network.
The hard disk and display devices can power down on a Windows XP computer after a certain period of inactivity. There are Power Options settings in the control panel that allow you to adjust the time-out periods for these devices.
It would be useful if your application could detect whether a device has powered down and if so, not access it. If the power source is a battery, the application could cause the device to power up whether you want it to or not.
There is a Win32 function that you can use to determine whether a device is powered up or not. GetDevicePowerState
takes two parameters: one to identify the device and the other that returns a Boolean indicating whether the device is powered up. This can be imported into managed code using the P/Invoke mechanism as follows:
[DllImport("Kernel32.DLL", CharSet = CharSet.Auto,
private extern static bool GetDevicePowerState(
IntPtr hDevice, out bool fOn);
This is a good way to find out if a disk is currently powered up. The method in Listing 3
shows how to retrieve information about whether the disk from which the assembly has been loaded is currently powered up.
cannot be used to find out if the display monitor is currently powered up, but this issue has been solved in Windows Vista, as you will discover at the end of this article.