free web page counters

Windows Mobile Pocket PC Smartphone Programming

==>Click here for the SiteMap<==. Original contents with decent amount of source codes.

Monday, December 05, 2005

Pocket PC Power Management Series 5: Force the Device not to Sleep: a Brutal Way?

====>SiteMap of this Blog<===

Pocket PC Power Management Series 5: Force the Device not to Sleep: a Brutal Way?

In Pocket PC Power Management Series 3 and Series 4, I demonstrated two ways to find out when the device is running, and when it is sleeping. The techniques is not much useful, unless you need to take certain actions when the device wakes up. Remember, there is NO WAY for you to take action when the device falls asleep. (You may write a driver, but that is completely out of my scope here.)

Now let us move into the more interesting topic, trying to solve the problem defined in Pocket PC Power Management Series 2. You need to write a media player, able to playing music without being suspended. Or, you need to write a stock alert program, able to monitor a stock symbol from time to time. The first case, case 1, is a continuously-running program, and the second, case 2, a periodically-running program. Apparently you do not want the OS to suspend your nicely running program abruptly, at least for the first case, the media player case.

Microsoft anticipates such need, and exposes an API:

void WINAPI SystemIdleTimerReset(void);

Basically this function resets a system timer that controls whether or not the device will automatically go into a suspended state. Let us say you set the device to power down in 3 minutes on battery power (via "Settings"=>"System"=>"Power"=>"Advanced"=>"Turn off device if not used for 3 minutes"). Each time this function is called, the device will run for another 3 minutes even if not used.

The frequency in which to call SystemIdleTimerReset in order to keep a device awake can be determined by retrieving the following registry key:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Power

Value "BattPowerOff" determines the seconds the device will be suspended on battery power (180 standing for 3 minutes), and value "ExtPowerOff" determines the seconds if on AC power (0 meaning no sleeping on AC).

Notice this API should be used cautiously for the following two reasons:

  • It should only be used for continuously-running programs (case 1), rather than periodically-running programs (case 2). It is a waste of battery if used for case-2 programs. For case-2 application, the omnipotent CeSetUserNotificationEx should be used (see my Pocket PC Power Management Series 4 for a brief introduction to this API).
  • Some utilities are known to disturb SystemIdleTimerReset. Keyguard is such one. Basically when your device is locked (unresponsive to any keypresses), Keyguard is placed to prevent you from hitting the device keys accidentally (while it is in your pocket) and prevent the backlight from coming on and drain the battery or making unwanted phone calls. If your program starts from sleep mode and the Keyaguard mode is on, then SystemIdleTimerReset does not work and the device suspend after specific time. Again the solution is to use CeSetUserNotificationEx but in a slightly more creative way.

Because of the nature of SystemIdleTimerReset, I call it a brutal way to force the device not to sleep. In next post, I plan to discuss how to use CeSetUserNotificationEx to more gently "request the device not to sleep", a good solution to case-2 programs, and case-1 programs as well.



Category: [Power Management]

====>SiteMap of this Blog<===