System Timer Considerations
A version of this page is also available for
4/8/2010
Avoid using the Sleep function to get greater timer precision in real-time threads. Instead, use a timer mechanism that is based on an interrupt or the multimedia timers.
The system timer generates system ticks at a fixed rate of one tick per millisecond, which is the rate at which a timer interrupt is generated and serviced by the OS. By generating a system tick every millisecond, Windows Mobile enables an application to sleep for approximately 1 millisecond when it invokes the Sleep function with a dwMilliseconds parameter value of 1. A thread can sleep for longer than a millisecond, depending on its priority in relation to other active threads and whether any interrup service routines (ISRs) are running. At a minimum, a Sleep(1) calls sleep for 1.z milliseconds, where z is the remaining time, less than a millisecond, until the next tick when the thread calls Sleep. A return from a Sleep call also depends upon your priority in relation to the other threads in the system. The APIs set the minimum time that a thread waits or sleeps.
The only kernel variables needed to determine whether a reschedule should occur is dwReschedTime. The following code example shows how to schedule a timer interrupt:
if ((int) (CurMSec – dwReschedTime) >= 0)
return SYSINTR_RESCHED;
else
return SYSINTR_NOP;