4

Does the Windows native API support timers?

I am aware that POSIX implementations on Windows support timers, but I am interested in Windows SDK APIs.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jay
  • 24,173
  • 25
  • 93
  • 141

4 Answers4

10

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

In particular you need to check

Incognito
  • 16,567
  • 9
  • 52
  • 74
4

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.

JSBձոգչ
  • 40,684
  • 18
  • 101
  • 169
  • 1
    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 May 18 '10 at 15:04
3

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

Alex K.
  • 171,639
  • 30
  • 264
  • 288
2

It's 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, it 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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Chris Becke
  • 34,244
  • 12
  • 79
  • 148