free web page counters

Windows Mobile Pocket PC Smartphone Programming

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

Sunday, January 08, 2006

Pocket PC Power Management Series 7: Trap of GetTickCount, and Sleep()

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

Pocket PC Power Management Series 7: Trap of GetTickCount, and Sleep()

If you come from desktop Windows world, very possibly you developed a habit to call GetTickCount() to keep track how long has elapsed, for example, to wait for a time interval to pass. Basically the API retrieves the number of milliseconds that have elapsed since the system was started. The elapsed time is stored as a DWORD value. Therefore, the time will wrap around to zero if the system is run continuously for 49.7 days. Although the warning message is printed everywhere in Microsoft documentation and in every tutorial, programmers love to use it, thinking they never need to track a task for as long as 59.7 days :)

What I want to emphasize to the desktop Windows programmers, is that GetTickCount() might fail miserably in Pocket PC devices. When the Pocket PC device is suspended, the internal timer is also suspended, thus stop increasing value. For example, at 10:00AM your program calls GetTickCount() to retrieve a DWORD value (time1), and the device runs unattended for 3 minutes then falls asleep; after another 5 minutes, the device wakes up, and your program resumes execution and calls GetTickCount() again to retrieve another DWORD value (time2). You may anticipate the difference between time2 and time1 equals to 8 minutes. Nope! The difference is actually 3 minutes, and you just missed 5 minutes that was when the device was sleeping.

One solution is to use GetLocalTime() API to retrieve the local time as ULONGLONG, then do arithmetic operation on two ULONGLONG values to calculate the real time elapsed. Notice GetLocalTime() has one headache: You need to take into account whether the end user changed the local time setting or not. To avoid such headache, you call use QueryPerformanceCounter() for similar purpose. Not surprisingly, QueryPerformanceCounter() has its own issue: It cannot sustain device reboot. So you need to make a trade off between these two choice.

Similar to the trap of GetTickCount(), desktop programmers also need to notice that Sleep() API cannot really sleep for the specified time period. Most probably, your thread will sleep longer, sometimes much longer. Please refer to my Pocket PC Power Management Series 3 for details, where I present a program making use of such sleep-longer-expected feature to determine whether the device was suspended or running.

Category: [Power Management]

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

[ [permalink] ]


At November 04, 2009 4:12 PM, Blogger Adi said...

Find Internet Marketing resource hare Online Marketing Strategy | Internet Marketing Tools | Online Marketing Campaign | Online Marketing Business | Online Marketing System | Online Business | Online Home Business | Online Business Tips | Internet Marketing Online


Post a Comment

Links to this post:

Create a Link

<< Home