Using SetTimer

Click here to change the theme.

This is a simple description of using CWnd::SetTimer. First, call CWnd::SetTimer, of course. The following will call CWnd::SetTimer with a time-out value of one second:

if (!SetTimer(nIDEvent, 1000, NULL))
	MessageBox("Error encountered setting timer");

Then use ClassWizard to add a message handler for the WM_TIMER message to the window that called CWnd::SetTimer.

Then when you are through with it, call CWnd::KillTimer:



If SetTimer and the WM_TIMER message are being used in a situation in which the WM_TIMER message is to be sent multiple times and if it is important that they be sent as close to a specified time as possible, then we can attempt to provide more precision by doing the following.

Set the timer for the amount of time until the beginning of the next minute, second or whatever and then in your WM_TIMER handler:

  • kill the current timer
  • calculate the amount of time until the beginning of the next minute or second or whatever
  • set the timer for that amount of time