Did you ever think about it? Waitabletimer is "Waiting", the previous example of the WaitForSingleObject wait function "is also waiting", which is "double wait", this is not good, too wasteful resources.
In fact, as a synchronization tool, the previous methods (event, signal, critical area) are basically enough; The function of Waitabletimer is not to repeat the previous function, its main function is similar to TTimer class; such as how often to execute a piece of code, or at a specified time to execute a piece of code.
Since have convenient ttimer, why use Waitabletimer again?
Because Waitabletimer is more accurate than ttimer, its interval can be accurate to milliseconds, its specified time, or even 0.1 milliseconds;
The TTimer-driven wm_timer message is the lowest priority in message queues, which means that wm_timer messages are always processed at the same time.
There is also an important point waitabletimer can be used across threads, across processes.
Continue to explore an important point: many times in order to let the thread does not conflict, the thread is waiting, since there are waiting, that waitabletimer very precise timing and what is the value of it? Thinking about the problem gives us a good understanding of the APC function.
SetWaitableTimer has a callback function (which is actually a procedure), and Windows requires it to be in the following format:
procedure TimerAPCProc(
lpArgToCompletionRoutine: Pointer;
dwTimerLowValue: DWORD;
dwTimerHighValue: DWORD
); stdcall;
The function name has the word APC, which indicates that this is an APC function (although this name does not matter, this is officially named), what is the APC function?
APC (Asyncroneus Procedure Call): Asynchronous procedure invocation.
In addition to each thread has a separate message queue, there is an APC queue (the APC function waiting to be executed); If the thread finds a situation in the APC queue, it immediately skips execution and then comes back to processing the message queue.
Speaking of trouble, when used only in the format of the above function to pass the pointer on the line; But there's a big difference between the callback functions that get into the APC queue and the other callback functions:
SetWaitableTimer after calling the APC function in a format, you need to see a "Wait" in the "Current thread" before the APC function can enter the queue.
This seems to be puzzling, for example: APC queues are so high priority, because the priority of the use of resources will have a great impact on other messages, certainly not random access, this is not like the life of the VIP seats or VIP aisle?
In other words, to enter the APC queue, only SetWaitableTimer calls are not enough, but also through the "Wait function" introduction.
WaitForSingleObject? No, it's not level enough; The following is a Windows approved wait function that can introduce APC row:
SleepEx();
WaitForSingleObjectEx();
WaitForMultipleObjectsEx();
MsgWaitForMultipleObjectsEx();
SignalObjectAndWait();