Original article address: http://www.intel.com/cd/ids/#/apac/zho/recent/325179.htmchallenge
Determine and minimize the impact of peekmessage calls on battery life in Windows *-based mobile apps. When talking about the optimization technology for applications running on battery power supply devices, some application developers may doubt or understand why they need to consider this issue.
Because peekmessage has been widely used as a key component of message loop in many old applications over the years, it is particularly important to solve the power problem in applications. Basically, peekmessage calls make the system busy checking messages and occupy a lot of the system.
Solution
Note the power prompt whenProgramEnable proper idle status when idle. You can write an application to make it more energy-efficient, allow windows to be idle, and monitor the approximate length of time when the unprocessed message of the application is idle. Because it is difficult to use peekmessage properly, we have designed a backup method to address potential problems of peekmessage.
One way to directly control peekmessage usage in an application is to build your own message loop. By ensuring that you have covered the basic elements in the Microsoft basic class * (MFC *) main message loop, your application can directly control the message pump. The following is a brief pseudo-concept compatible with MFC:CodeExample:
While (bdoingbackgroundprocessing) {MSG; while (: peekmessage (& MSG, null, 0, 0, pm_noremove) {If (! Pumpmessage () {bdoingbackgroundprocessing = false;: postquitmessage (); break ;}// enable MFC to perform idle processing for long minutes le = 0; while (afxgetapp () -> onidle (lidle ++); // perform some background processing here // use another onidle call} |
In this Code, as long as there is idle processing in progress, peekmessage is called. Pumpmessage is used for normal message conversion and scheduling. The problem with using this code is that pumpmessage is called. When Source code It can be found in thrdcore. cpp without being formally archived, so it may change in later versions.
The problem with peekmessage in a loop is that Windows cannot enter idle state. The energy-saving application processes all messages and processes them in the background. Then, it calls waitmessage to go to sleep until a new message is displayed. You can use waitmessage to release the system to process other tasks:
If (peekmessage (...)! = NULL) // If a message exists, the else if (there is background processing to do) of the message will be converted and scheduled. // The else is processed in the background without any background processing, no waiting message-enter sleep status waitmessage (); |
Write Applications to achieve self-management, as shown in the pseudo code above, which helps improve the power usage of the battery power supply system.