Power to the People

Power to the People

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.

Limited Power
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. 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 and FullBatteryLifeTime fields may not be available. For this reason, it is often best to use the percentage of battery life remaining.)

Power Play
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,      SetLastError=true)]   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.

GetDevicePowerState 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.

Notifications
Knowing the current power source is a good start, but your application then needs to be alerted when the power source changes. It is also important to be alerted when the computer is going to suspend or hibernate and when it wakes up or restores its state after a suspend or hibernate.

In the .NET Framework 1.1, there is a PowerModeChanged event in the SystemEvents class in the Microsoft.Win32 namespace. The PowerModeChangedEvent passes you a PowerModeChangedEventArgs object containing a Mode property that is a PowerModes enumerated type. The possible PowerModes you might receive are Resume, StatusChange, and Suspend.

It is obvious what the Resume and Suspend events indicate, but the StatusChange event can indicate any change in the current state of the power. The changes that might be indicated include a switch from AC to DC, a switch from DC to AC, or a change in the state of the battery (for example, indicating that the battery has only a low charge remaining).

When you receive a PowerModeChanged event with the StatusChange flag, it would be good practice to call the GetSystemPowerStatus method discussed earlier to find out what specifically has changed.

The PowerModeChanged event provides a subset of information that can be received from the WM_POWERBROADCAST Windows message. This whitepaper on MSDN discusses how to use the WM_POWERBROADCAST message, but for most applications, using the PowerModeChanged event should be sufficient.

Being Power Efficient
The user experience on a Mobile PC can be greatly improved by making sure your software does not consume more battery power than is needed. You can monitor this by carefully considering the requirements of your application when it runs on a computer-powered by battery.

Some operations, such as audio output or graphics-intensive operations, are obvious power gluttons. Less obvious suspects are the impact that use of the hard disk and network have on battery life. In fact, all CPU operations have some impact on battery life and if your application is making excessive use of polling or background threads, these will also degrade performance of the battery.

Often, these are not purely technical decisions; the business decision makers need to be involved to understand the impact of requested features on battery life. It is good practice to avoid carrying out non-critical tasks on a computer that is currently powered only by battery.

Suspension of Operations
The consideration so far has been how to reduce power consumption when the computer is powered by batteries. There is one other important consideration to take into account: how your application should behave when the computer suspends. Suspending the computer is the primary way Windows helps extend mobile PC battery life when the computer is not in use.

To the end user, the resume experience should be seamless; it should appear as if the application is simply running again.

There are two scenarios to consider that will cause the computer to go into suspend mode. The first is when the computer has been idle for a set time period and the Power Options are set for the PC to stand by or hibernate after that period of inactivity. The second scenario is when the user shuts the lid or pushes the power button, which causes a request to suspend operations.

When the operating system suspends, it sends out the suspend notification message, which you can receive in the PowerModeChanged event. Your application then has to stop any limited cleanup actions and store any important state data as quickly as possible.

You should never request any user input on suspend. There is nothing more annoying to a user who closes the laptop’s lid to find that the computer did not suspend because your application was waiting for a response from a MessageBox and the battery has drained.

Once you have worked out how your application responds to a suspend event, you should consider how it behaves upon receiving a resume notification. To the end user, the resume experience should be seamless; it should appear as if the application is simply running again.

The devices attached to the computer when it suspends may have changed by the time the computer resumes operations. The computer may have been docked, undocked, had other peripherals changed or have a different network connectivity. Your application has to take account of this change and act accordingly, without interrupting the user or using a popup dialog box, if at all possible.

Your application must never veto a suspend request.

Your application must never veto a suspend request. Once the computer has sent a notification to suspend, your application must comply. This becomes even more important in Windows Vista, where your application will be given a maximum of two seconds to respond to a suspend notification before the operating system suspends, and vetoing a second request is not possible.

Although your application cannot do anything once the suspend request has been sent out, you may wish to prevent the computer from requesting a suspend request based on idle time.

For example, if your application is playing a video in a window, it is likely that while the user is watching that video, they will not be interacting with the computer. After a certain period of inactivity, the idle timer requests that the computer suspend. You can prevent the idle timer from triggering a request to suspend by using the Win32 SetThreadExecutionState API. You should only do this when it is absolutely necessary, as it will cause degradation in battery life.

As the name suggests, SetThreadExecustionState is per thread. The operating system maintains a count of threads that have requested a certain state, like perhaps the display is required. Only when each of these threads has indicated that they no longer require the resource will the resource be allowed to suspend. SetThreadExecutionState does not prevent the system from suspending when it is user-initiated.

The SetThreadExecutionState function and the EXECUTION_STATE types can be defined in C# as shown in the code snippet below:

   [FlagsAttribute]       public enum EXECUTION_STATE :uint   {      ES_SYSTEM_REQUIRED  = 0x00000001,      ES_DISPLAY_REQUIRED = 0x00000002,      // legacy flag should not be used      // ES_USER_PRESENT   = 0x00000004,      ES_CONTINUOUS       = 0x80000000,   }   [DllImport("Kernel32.DLL", CharSet = CharSet.Auto,      SetLastError=true)]   private extern static   EXECUTION_STATE SetThreadExecutionState(      EXECUTION_STATE state);

Dealing with Power On Windows Vista
Windows Vista improves how your application can interact with Windows’ power management. The WM_POWERBROADCAST message is still sent to notify the application of changes. This does mean that there needs to be a Window handle to receive these messages.

Windows Vista then provides a number of GUIDs that you can use to register to receive information about particular features of the power. For example, if you want your application to be notified of the power source, register for the GUID_ACDC_POWER_SOURCE GUID.

The registration method is called RegisterPowerSettingNotification and takes a GUID parameter that identifies the notification for which you are registering.

   hPowerSrc = RegisterPowerSettingNotification(      this.Handle, ref GUID_ACDC_POWER_SOURCE,      DEVICE_NOTIFY_WINDOW_HANDLE);

The returned handle should then be used to unregister the notification.

Windows Vista improves how you can interact with the power management APIs.
   UnregisterPowerSettingNotification(hPowerSrc);

When the notification messages are sent, they are accompanied by a structure that is passed in the lParam of the Windows message. This structure is a POWERBROADCAST_SETTING, defined here in C#.

   [StructLayout(LayoutKind.Sequential, Pack = 4)]   internal struct POWERBROADCAST_SETTING   {      public Guid PowerSetting;      public Int32 DataLength;   }

The PowerSetting field in the structure identifies the setting or event notification you are receiving. Remember, you can register to be notified of changes to multiple power settings.

In Windows Vista, it is easy for the user to change the active Power Plan. A Power Plan specifies the power management settings in effect on the computer. The three power plans that will ship with Windows Vista are Automatic, High Performance, and Power Saver.

As the names suggest, these power plans indicate to the running application how to behave regarding power consumption. In Automatic mode, it is best practice to scale functionality based on criteria, such as application demand, current power source and battery percentage remaining. In High Performance, your application should not scale back functionality to improve battery life; the user has chosen not to worry about battery life and your application should focus on providing the best possible performance. When the user chooses Power Saver, your application should scale back all non-critical functionality to reduce power consumption.

Your application can register for a change in the Power Plan by calling the RegisterPowerSettingNotification method using the GUID_POWERSCHEME_PERSONALITY GUID.

As you can see, it’s wise to add power management to your application. Doing this is not hard and can help to improve the user experience by extending the battery life of the device on which your application is run.

To prevent the user experiencing a drained battery, your application should always respond in a timely manner to suspend requests and never block the computer from suspending.

Here is a list of some of the things you should consider when building your application:

  • Determine the power source.
  • Never prevent a request for the system to suspend.
  • When the system resumes, your application should run smoothly, as if it had never been interrupted.
  • When the system resumes, the available devices and power source may have changed. Your application should be reasonably unaffected by these changes or have built-in contingency plans.
  • Reduce the power consumption of your application when the power source is a battery by avoiding polling operations, not running non-critical worker threads, and avoiding unnecessary device (disk, display, Bluetooth, USB) access.
devx-admin

devx-admin

Share the Post:
Economy Act Soars

Virginia’s Clean Economy Act Soars Ahead

Virginia has made significant strides towards achieving its short-term carbon-free objectives as outlined in the Clean Economy Act of 2020. Currently, about 44,000 megawatts (MW)

Renewable Storage Innovation

Innovative Energy Storage Solutions

The Department of Energy recently revealed a significant investment of $325 million in advanced battery technologies to store excess renewable energy produced by solar and

Development Project

Thrilling East Windsor Mixed-Use Development

Real estate developer James Cormier, in collaboration with a partnership, has purchased 137 acres of land in Connecticut for $1.15 million with the intention of

USA Companies

Top Software Development Companies in USA

Navigating the tech landscape to find the right partner is crucial yet challenging. This article offers a comparative glimpse into the top software development companies

Software Development

Top Software Development Companies

Looking for the best in software development? Our list of Top Software Development Companies is your gateway to finding the right tech partner. Dive in

Economy Act Soars

Virginia’s Clean Economy Act Soars Ahead

Virginia has made significant strides towards achieving its short-term carbon-free objectives as outlined in the Clean Economy Act of 2020. Currently, about 44,000 megawatts (MW) of wind, solar, and energy

Renewable Storage Innovation

Innovative Energy Storage Solutions

The Department of Energy recently revealed a significant investment of $325 million in advanced battery technologies to store excess renewable energy produced by solar and wind sources. This funding will

Renesas Tech Revolution

Revolutionizing India’s Tech Sector with Renesas

Tushar Sharma, a semiconductor engineer at Renesas Electronics, met with Indian Prime Minister Narendra Modi to discuss the company’s support for India’s “Make in India” initiative. This initiative focuses on

Development Project

Thrilling East Windsor Mixed-Use Development

Real estate developer James Cormier, in collaboration with a partnership, has purchased 137 acres of land in Connecticut for $1.15 million with the intention of constructing residential and commercial buildings.

USA Companies

Top Software Development Companies in USA

Navigating the tech landscape to find the right partner is crucial yet challenging. This article offers a comparative glimpse into the top software development companies in the USA. Through a

Software Development

Top Software Development Companies

Looking for the best in software development? Our list of Top Software Development Companies is your gateway to finding the right tech partner. Dive in and explore the leaders in

India Web Development

Top Web Development Companies in India

In the digital race, the right web development partner is your winning edge. Dive into our curated list of top web development companies in India, and kickstart your journey to

USA Web Development

Top Web Development Companies in USA

Looking for the best web development companies in the USA? We’ve got you covered! Check out our top 10 picks to find the right partner for your online project. Your

Clean Energy Adoption

Inside Michigan’s Clean Energy Revolution

Democratic state legislators in Michigan continue to discuss and debate clean energy legislation in the hopes of establishing a comprehensive clean energy strategy for the state. A Senate committee meeting

Chips Act Revolution

European Chips Act: What is it?

In response to the intensifying worldwide technology competition, Europe has unveiled the long-awaited European Chips Act. This daring legislative proposal aims to fortify Europe’s semiconductor supply chain and enhance its

Revolutionized Low-Code

You Should Use Low-Code Platforms for Apps

As the demand for rapid software development increases, low-code platforms have emerged as a popular choice among developers for their ability to build applications with minimal coding. These platforms not

Cybersecurity Strategy

Five Powerful Strategies to Bolster Your Cybersecurity

In today’s increasingly digital landscape, businesses of all sizes must prioritize cyber security measures to defend against potential dangers. Cyber security professionals suggest five simple technological strategies to help companies

Global Layoffs

Tech Layoffs Are Getting Worse Globally

Since the start of 2023, the global technology sector has experienced a significant rise in layoffs, with over 236,000 workers being let go by 1,019 tech firms, as per data

Huawei Electric Dazzle

Huawei Dazzles with Electric Vehicles and Wireless Earbuds

During a prominent unveiling event, Huawei, the Chinese telecommunications powerhouse, kept quiet about its enigmatic new 5G phone and alleged cutting-edge chip development. Instead, Huawei astounded the audience by presenting

Cybersecurity Banking Revolution

Digital Banking Needs Cybersecurity

The banking, financial, and insurance (BFSI) sectors are pioneers in digital transformation, using web applications and application programming interfaces (APIs) to provide seamless services to customers around the world. Rising

FinTech Leadership

Terry Clune’s Fintech Empire

Over the past 30 years, Terry Clune has built a remarkable business empire, with CluneTech at the helm. The CEO and Founder has successfully created eight fintech firms, attracting renowned

The Role Of AI Within A Web Design Agency?

In the digital age, the role of Artificial Intelligence (AI) in web design is rapidly evolving, transitioning from a futuristic concept to practical tools used in design, coding, content writing

Generative AI Revolution

Is Generative AI the Next Internet?

The increasing demand for Generative AI models has led to a surge in its adoption across diverse sectors, with healthcare, automotive, and financial services being among the top beneficiaries. These

Microsoft Laptop

The New Surface Laptop Studio 2 Is Nuts

The Surface Laptop Studio 2 is a dynamic and robust all-in-one laptop designed for creators and professionals alike. It features a 14.4″ touchscreen and a cutting-edge design that is over

5G Innovations

GPU-Accelerated 5G in Japan

NTT DOCOMO, a global telecommunications giant, is set to break new ground in the industry as it prepares to launch a GPU-accelerated 5G network in Japan. This innovative approach will

AI Ethics

AI Journalism: Balancing Integrity and Innovation

An op-ed, produced using Microsoft’s Bing Chat AI software, recently appeared in the St. Louis Post-Dispatch, discussing the potential concerns surrounding the employment of artificial intelligence (AI) in journalism. These

Savings Extravaganza

Big Deal Days Extravaganza

The highly awaited Big Deal Days event for October 2023 is nearly here, scheduled for the 10th and 11th. Similar to the previous year, this autumn sale has already created

Cisco Splunk Deal

Cisco Splunk Deal Sparks Tech Acquisition Frenzy

Cisco’s recent massive purchase of Splunk, an AI-powered cybersecurity firm, for $28 billion signals a potential boost in tech deals after a year of subdued mergers and acquisitions in the

Iran Drone Expansion

Iran’s Jet-Propelled Drone Reshapes Power Balance

Iran has recently unveiled a jet-propelled variant of its Shahed series drone, marking a significant advancement in the nation’s drone technology. The new drone is poised to reshape the regional