Timers

A timer is a system resource that can notify an application at regular intervals. An application associates a timer with a window and sets the timer for a specific time-out period. Each time the specified interval, or time-out value, for a specified timer elapses, the system uses a WM_TIMER message to notify the window associated with the timer. Because the accuracy of a timer depends on the system clock rate and how often the application retrieves messages from the message queue, the time-out value is only approximate. The smallest possible interval a timer can measure is the system tick interval.

You use the SetTimer function to create a timer. The timer may be associated with a particular window or with just the thread. If you associate the timer with a window, then normal message loop processing will cause the WM_TIMER message to be dispatched to the window's window procedure. If you do not associate the timer with a window, then you must design the message loop to recognize and handle the WM_TIMER message.

If the call to SetTimer includes a TimerProc callback function, the procedure is called when the timer expires. This call is done inside the GetMessage or PeekMessage function. This means that a thread must be executing a message loop to service a timer, even if you are using a timer callback procedure.

A new timer starts timing its interval as soon as it is created. An application can change a timer's time-out value by using the SetTimer function, and it can destroy a timer by using the KillTimer function. To use system resources efficiently, applications should destroy timers that are no longer necessary.

You can use the timer and window identifiers to identify timers associated with a window. You can identify timers that are not associated with a particular window by using the identifier returned by the SetTimer call.

Timer messages have a low priority in the message queue. Although you know that the window associated with a timer is notified sometime after the timer interval expires, you cannot know the exact time it will receive the notification.

Timers expire at regular intervals, but a timer that expires multiple times before being serviced does not generate multiple WM_TIMER messages.