11

How to create timer in WinApi (C++)?

5 Answers 5

11

Call the SetTimer function. This allows you to specify a callback function, or to have Windows post you a WM_TIMER message.

Sign up to request clarification or add additional context in comments.

2 Comments

as per nobugz - this will not work in console applications or non gui threads. threads must be pumping messages to generate SetTimer callbacks.
SetTimer is not a high resolution (i.e., <20ms) timer. CreateTimerQueueTimer is better but consume more resources.
7

You cannot not know this if you write GUI code. Which makes it likely you want to use CreateTimerQueueTimer().

2 Comments

Yes, CreateTimerQueueTimer is your friend. Beware that the callback is executed on a threadpool thread so use proper locking.
CreateWaitableTimer can also be used in console application. msdn.microsoft.com/en-us/library/windows/desktop/…
4

SetTimer. A window handle is needed, and the timer will not be delivered if you aren't pumping messages.

Comments

4

A Good Example for CreateTimerQueueTimer : Here

Another is HERE

Comments

2

call the setTimer() Function. Suppose I called

SetTimer(hWnd,POST_CBIT_TIMER,500,NULL);

Call back function is

UINT nIdEvent ;//global member variable

case WM_TIMER:

if(nIDEvent == POST_CBIT_TIMER)
{

KillTimer(hParent,POST_CBIT_TIMER);


}
break;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.