9.4 A waiting Timer kernel object-- a specified time or a time interval that is triggered once
(1) Create a waiting timer: CreateWaitableTimer ( The constant _WIN32_WINNT should be defined as 0x0400when used)
Parameters |
Describe |
Psa |
Security attributes (such as use count, handle inheritance, and so on) |
bManualReset |
Resets the timer manually or automatically resets the timer. ① when the manual timer is triggered , all threads waiting for the timer are changed to be available for dispatch. ② when an automatic timer is triggered , only one thread that is waiting for the counter becomes available for dispatch |
Pszname |
The name of the object |
(2) You can also open a waiting timer that has already been available: Openwaitabletimer
(3) Set the waiting timer state: SetWaitableTimer
Parameters |
Describe |
HANDLE Htimer |
The timer to trigger |
large_integer* Pduetime |
The time the timer was triggered for the 1th times ( UTC should be coordinated for the world) Note:Pduetime is an absolute time when it is positive . is a negative number, represents a relative time , indicating how many (100ns) milliseconds should trigger the timer for the 1th time relative to the call of the function. If after 5 seconds, it should be -5*10 000 000 |
LONG Lperiod |
When the first trigger occurs, It is triggered once every few seconds (in microseconds). If you want the timer to fire only once and then no longer touch, the parameter is 0. |
Ptimerapcroutine PFNCR |
callback function to join the APC queue |
PVOID PVARGTOCR |
Additional parameters passed to the callback function |
BOOL Bresume |
If true, and the system supports power management, the system exits the battery saving mode when the timer is triggered. If set to true, but the system does not support power saving mode, GetLastError will return to the error_not_supported applicable platform. Generally set to False |
(4) Cancel timer: Cancelwaitabletimer, after the call timer will never be triggered.
"SetWaitableTimer Pseudo-code"-- set timer to trigger on August 18, 2015 14:00, trigger every 6 hours
HANDLE Htimer; SYSTEMTIME St= {0 }; FILETIME ftlocal, FTUTC; Large_integer LIUTC;//to create an automatic timer objectHtimer =CreateWaitableTimer (NULL, FALSE, NULL);//first, the setup time is August 18, 2015, 14:00 (local time)St.wyear = -; St.wmonth=8; St.wday= -; St.whour= -;//pm FormatSt.wminute =0; St.wsecond=0; St.wmilliseconds=0; SystemTimeToFileTime (&st, &ftlocal);//Convert local time to UTC timeLocalFileTimeToFileTime (&ftlocal, &FTUTC);//convert filetime to Large_integer (because of different alignment)//the address of the FILETIME structure must be an integer multiple of 4 (32-bit boundary),//the address of the Larg_integer structure must be an integer multiple of 8 (64-bit boundary)Liutc.lowpart =Ftutc.dwlowdatetime;liutc.highpart=Ftutc.dwhighdatetime;//Setting Timers//SetWaitableTimer incoming time is always UTC time (this time must be a 64-bit boundary)//triggers once every 6 hours after LIUTCSetWaitableTimer (Htimer, &LIUTC,6* -* -* +, NULL, NULL, FALSE);
9th. Thread synchronization with kernel objects (2) _ Wait timer (Waitabletimer)