tags:

views:

113

answers:

4

Do Windows Native API support Timers? I am aware that POSIX implementations on Windows support timer, but I am interested in Windows SDK APIs.

+4  A: 

It sure does: http://windows-programming.suite101.com/article.cfm/using_the_win32_timer_api

The SetTimer API mentioned in that article depends on the WM_TIMER message, which means that you have to have a message loop, which means that you (probably) have to have a window. So it's very useful for GUI programming, less so for command-line tools.

JSBangs
The window for your message sink doesn't need to be visible, so it can be used in any type of app. You really can't do much with WinAPI without having a window.
Alan
+10  A: 

Yes there are timers in Win32 API. More details you can check here : Timers

In particular you need to check

Incognito
+3  A: 

In addition to the timers described above there is also the high-resolution timeSetEvent (Multimedia API) & CreateTimerQueueTimer .

Alex K.
+1  A: 

Its a tricky question to answer in the context of a POSIX timer. The Window API SetTimer creates a timer on a GUI thread that relies on the thread's message queue dispatching mechanism - which means somewhere in the thread you are calling GetMessage / DispatchMessage.

If you are writing non GUI code, having to implement a message loop is an unnatural constraint :- The Windows kernel uses synchronization objects (in place of signals) as a way for worker (i.e. non GUI) threads to be alerted to events. CreateWaitiableTimer will create a handle that can be passed to WaitForSingleObject / WaitForMultipleObjects in a worker thread.

Alternately, you can create a worker thread - implement a timer (gui or kernel) in that, and simply call into your (obviously must be a thread safe) object as the timer is signaled.

The choice really depends on exactly how POSIX-like your application is going to be.

Chris Becke