Your browser doesn't support JavaScript Timers – Windows Programming

Timers

Timers allow an application to execute code at regular time intervals without requiring continuous user interaction. They are commonly used for tasks such as updating clocks, creating animations, polling hardware, refreshing displays, or performing periodic background processing.

The CWnd member function SetTimer() starts a timer that generates events at a specified interval, while the KillTimer() member function stops a previously created timer.

A timer can notify an application in one of two ways:

  • By sending a WM_TIMER message to a window.
  • By calling an application-defined callback function.

The prototype for CWnd::SetTimer() is:

UINT_PTR SetTimer(
    UINT_PTR nIDEvent,
    UINT nElapse,
    TIMERPROC lpfnTimer = NULL
);

Where

  • nIDEvent specifies the timer identifier. This value is used to distinguish between multiple timers associated with the same window. (Traditionally this should be non-zero.)
  • nElapse specifies the timer interval in milliseconds.
  • lpfnTimer specifies the address of an application-defined callback function. If this parameter is NULL, the timer generates WM_TIMER messages that are placed in the application’s message queue.

The function returns the timer identifier if successful; otherwise it returns zero.


Example

SetTimer(1, 700, NULL);

This statement creates a timer with an identifier of 1 that generates a WM_TIMER message every 700 milliseconds. Because the callback parameter is NULL, the timer communicates by sending WM_TIMER messages rather than calling a callback function.


Responding to WM_TIMER Messages

When a timer generates WM_TIMER messages, MFC routes them through the ON_WM_TIMER() message-map macro to the OnTimer() member function.

The prototype is:

afx_msg void OnTimer(UINT_PTR nIDEvent);

where nIDEvent identifies the timer that generated the message.

A typical implementation is shown below.

void CMainFrame::OnTimer(UINT_PTR nIDEvent)
{
    if (nIDEvent == 1)
    {
        // Timer processing code
    }

    CFrameWnd::OnTimer(nIDEvent);
}

Using a Callback Function

Instead of generating WM_TIMER messages, a timer can call an application-defined callback function directly.

For example:

SetTimer(ID_TIMER, 500, TimerCallBackProc);

The callback function is declared as follows:

void CALLBACK TimerCallBackProc(
    HWND hwnd,
    UINT uMsg,
    UINT_PTR idEvent,
    DWORD dwTime
);

Where:

  • hwnd contains the handle of the associated window.
  • uMsg contains the message identifier (WM_TIMER).
  • idEvent contains the timer identifier.
  • dwTime specifies the number of milliseconds that have elapsed since Windows was started.

Timer callback functions are typically used when timer processing does not need to be associated directly with a window’s message handler.


Stopping a Timer

When a timer is no longer required, it should be destroyed by calling KillTimer().

For example:

KillTimer(1);

This statement stops the timer whose identifier is 1. Once the timer has been destroyed, no further WM_TIMER messages or callback notifications will be generated for that timer.

Note: Windows timers are intended for general-purpose timing and are not guaranteed to fire at the exact requested interval. Timer messages are processed through the application’s message queue and may be delayed if the system is busy or the application is processing other messages. They are therefore unsuitable for high-precision timing applications such as multimedia playback or real-time control systems.

Example

The following program illustrates a simple timer app by flashing a “hello world” message in the top left-hand corner of the window