1st
In multithreaded programming, I hope that each thread can create a timer, and the first thing that comes to my mind is the SetTimer
function.
But in the Windows documentation HWND
parameter have this sentence” This window must be owned by the calling thread”
My understanding is that other worker threads can only create new Timers with HWND
as Null parameter
The problem here is: I want to pass a parameter to the callback function through nIDEvent
, but if HWND
is NULL, nIDEvent
does not seem to be customizable
A nonzero timer identifier. If the hWnd parameter is NULL, and the nIDEvent does not match an existing timer then it is ignored and a new timer ID is generated. If the hWnd parameter is not NULL and the window specified by hWnd already has a timer with the value nIDEvent, then the existing timer is replaced by the new timer. When SetTimer replaces a timer, the timer is reset. Therefore, a message will be sent after the current time-out value elapses, but the previously set time-out value is ignored. If the call is not intended to replace an existing timer, nIDEvent should be 0 if the hWnd is NULL.
2nd
I try to use CreateTimerQueueTimer
. It looks simpler than the SetTimer
function, But other problems arise.
HANDLE HWD1, HWD2 ;
bool result1 = CreateTimerQueueTimer(&HWD1, NULL, CB, &data1, Time, 0, WT_EXECUTEINTIMERTHREAD | WT_EXECUTEONLYONCE);
bool result2 = CreateTimerQueueTimer(&HWD2, NULL, CB, &data2, Time + 10, 0, WT_EXECUTEINTIMERTHREAD | WT_EXECUTEONLYONCE);
Two timers are set in multithreading. Timer 2
is 10ms longer than Timer 1
. I hope that the timer of each thread is independent.
But I found that the callback function of Timer 2
is earlier than that of Timer 1
.
I don’t know where my thinking is wrong, please help