Share via


Creating Timers (Windows CE 5.0)

Send Feedback

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 that a timer can measure is the system tick interval.

To create a timer, call the SetTimer function. The timer can be associated with a particular window or with just the thread. If you associate the timer with a window, message loop processing will cause the WM_TIMER message to be dispatched to the window procedure for the window. If you do not associate the timer with a window, you must design the message loop to recognize and handle the WM_TIMER message.

The following code example shows how to create a timer and associate the time with a window by using the SetTimer function.

SetTimer(hWnd, ID_TIMER1, 2000, NULL);

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.

The following code example shows how to include a callback function when you call SetTimer.

SetTimer(hWnd, ID_TIMER2, 1000, (TIMERPROC)Timer2Proc);

The following code example shows a sample TimerProc callback function that draws text in a window and makes a sound when the function is called after the timer expires.

VOID CALLBACK Timer2Proc(
                        HWND hWnd, // handle of window for timer messages
                        UINT uMsg,    // WM_TIMER message
                        UINT idEvent, // timer identifier
                        DWORD dwTime  // current system time
                        )
{
  RECT rt;

  // Clear the window.
  InvalidateRect(hWnd, NULL, TRUE);
  UpdateWindow(hWnd);
  HDC hdc = GetDC(hWnd);
  GetClientRect(hWnd, &rt);
  MessageBeep(MB_ICONQUESTION);
  DrawText(hdc, TEXT("   Timer2 - Did you hear it?   "), 
          _tcslen(TEXT("   Timer2 - Did you hear it?   ")), &rt, 
          DT_SINGLELINE | DT_VCENTER | DT_CENTER);
  ReleaseDC(hWnd, hdc);
  return;
}

A new timer starts timing its interval as soon as it is created. An application can change a time-out value for a timer by calling the SetTimer function, and it can destroy a timer by calling the KillTimer function. To use system resources efficiently, applications should destroy unnecessary timers.

The following example shows how to destroy a timer by calling KillTimer.

KillTimer(hWnd, ID_TIMER1);

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

Timer messages have 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.

See Also

Using Resources | GWES Application Development

Send Feedback on this topic to the authors

Feedback FAQs

© 2006 Microsoft Corporation. All rights reserved.