Pocket PC Power Management Series 3: When is My Device Sleeping, and when is it Running?
====>SiteMap of this Blog<===Pocket PC Power Management Series 3: When is My Device Sleeping, and when is it Running?
I have seen too many people asking these questions: I want to query the OS to determine how much time before the device goes into sleep. Or, I want to get notified when the device is going to sleep. Well, conceptually it is not possible. Let us say OS generates such an event that it is going to sleep, and you write an event listener to perform some tasks. Guess what? Your event listener even does not get a chance to run, because the device falls asleep!
To help understand the pattern how the device runs-sleeps-runs-sleeps, I wrote a program that records when the device falls sleep and prints out a simple diagram showing what time the device is running, and what time the device is sleeping. You can download the program from your Pocket IE, or from your desktop browser then copy it to your device. The program can only run in Windows Mobile devices, not on desktop.
Below are three screen cuts taken when the program runs in my device. Each screen cut shows how long the device runs and how long it sleeps, and draw two lines reflecting when it runs and when it sleeps. Take a closer look at the third screen cut, when the device was put onto my table unattended for a night. The device only runs for 6614/63598=10% of the time. That is Microsoft's way to save battery, and ironically, it did not work well for a few programs, for example, the ones I am trying to tackle in my Power Management series...
How the program works? Basically there are two threads. One thread is responsible for updating the UI based on an internal data structure. The other thread simply sleeps periodically at a predefined interval; each time it wakes up, the thread tests whether it has slept through the predefined interval. If the device is suspended, the sleeping thread could find that it has slept for a time longer than expected, so it figured out the device must have fall asleep, and update the internal data structure accordingly. Notice this is an inefficient way to test whether a device is slept. I plan to discuss a much better one in my next post.
APIs and technique used in the program:
- Get the current time in seconds, using GetLocalTime and SystemTimeToFileTime
- Compare two ULONGLONG structures. A ULONGLONG is essentially two DWORDs.
- Basic threading code: CreateThread, and how to gracefully shutdown a sleeping or running thread
- Basic thread communication mechanism: CreateEvent, PulseEvent, WaitForSingleObject
- InterruptibleSleep: a sleep that can be gracefully interrupted at anytime by using WaitForSingleObject, rather than the plain vanilla Sleep()
- Compare two ULONGLONG structures. A ULONGLONG is essentially two DWORDs.
- Basic threading code: CreateThread, and how to gracefully shutdown a sleeping or running thread
- Basic thread communication mechanism: CreateEvent, PulseEvent, WaitForSingleObject
- InterruptibleSleep: a sleep that can be gracefully interrupted at anytime by using WaitForSingleObject, rather than the plain vanilla Sleep()
Category: [Power Management]
0 Comments:
Post a Comment
<< Home