Are timers supported by the Windows API?

Does the built-in Windows API support timers?

I know that POSIX implementations on Windows support timers, but I'm interested in the Windows SDK APIs.

+4
source share
4 answers

This is a tough question to answer in the context of a POSIX timer. The SetTimer window API creates a timer in the GUI thread that relies on the dispatch mechanism of the thread's message queue — which means somewhere in the thread that you call GetMessage / DispatchMessage.

If you are not writing a GUI code, the need to implement a message loop is an unnatural restriction: - The Windows kernel uses synchronization objects (instead of signals) as a way for worker threads (that is, not a GUI) to notify about events. CreateWaitableTimer will create a handle that can be passed to WaitForSingleObject / WaitForMultipleObjects in the workflow.

Alternatively, you can create a workflow - implement a timer in it (GUI or kernel) and simply call an object in your (obviously, it should be thread safe) when the timer is signaled.

The choice really depends on how POSIX will look like your application.

+2
source

Yes, there are timers in the Win32 API. You can find out more information here: Timers

In particular, you need to check

+10
source

He's sure: http://windows-programming.suite101.com/article.cfm/using_the_win32_timer_api

The SetTimer API mentioned in this article depends on the WM_TIMER message, which means you should have a message loop, which means you (probably) should have a window. Therefore, it is very useful for programming a graphical interface, moreover, for command line tools.

+4
source

In addition to the timers described above, there is also timeSetEvent (Multimedia API) and CreateTimerQueueTimer .

+3
source

Source: https://habr.com/ru/post/1310128/


All Articles