1 can wait for a timer2 A wait timer is a kernel object that is triggered at a specified time or triggered at intervals. 5 Let's take a look at creating a wait timer function:6 7 HANDLE CreateWaitableTimer (8 lpsecurity_attributes Lptimerattributes,9 BOOL bManualReset,Ten LPCSTR lptimername); One first to third parameter I don't think I need to introduce you. Ha, which is the basic parameters of creating kernel objects. A The second parameter, bManualReset, indicates whether to create a manual reset (TRUE) or an automatic reset (FALSE) timer. - one thing to remember: Creating a waiting timer is always in the non-triggering state. You must call the SetWaitableTimer function to tell it. - the BOOL SetWaitableTimer ( - HANDLE Htimer, - ConstLarge_integer *Lpduetime, - LONG Lperiod, + Ptimerapcroutine Pfncompletionroutine, - lpvoid Lpargtocompletionroutine, + BOOL fresume); A the first parameter, Htimer, represents the timer we want to set. at the second and third parameters should be used together, Lpduetime indicates when the first trigger should be, - Lperiod indicates the frequency at which the timer should be triggered. The unit of the third parameter is microseconds - The other two parameters are not described here first, if interested can read "Windows core Programming (Fifth Edition)" 245 pages or refer to MSDN. - if it is just a simple application, the following three parameters, followed by filling in Null,null,false can, hey, simple! - - in Special attention: - If the timer is reset manually, then all threads that are waiting for that thread become ready to be dispatched when the timer is automatically reset, toThen only one timer waiting on the thread becomes available for dispatch. When the timer is automatically reset, whenever the waitfor*Object + when the object is acquired, it is re-timed and the manual reset does not re-clock, which is especially important. - the Let's talk about the use steps * 1) $ //Define a Timer kernel object, and some variables about timePanax Notoginseng HANDLE G_htimer; - SYSTEMTIME St; the + FILETIME FTLOCAL,FTUTC; A the Large_integer LIUTC; + 2) - //Create a Timer kernel object that resets automatically $G_htimer =CreateWaitableTimer (null,false,null); $ - 3Set the Timer kernel object, the time can be more according to their actual situation set - //The first response was in 2014.08.10 at 17:12 minutes the -St.wyear = the ;WuyiSt.wmonth =8 ; the //St.wdayofweek = 0//can be fooled -St.wday =Ten ; WuSt.whour = - ; -St.wminute = A; AboutSt.wsecond =0 ; $St.wmilliseconds =0 ; - -SystemTimeToFileTime (&st,&ftlocal); - ALocalFileTimeToFileTime (&ftLocal,&FTUTC); + theLiutc.lowpart =Ftutc.dwlowdatetime; -Liutc.highpart =Ftutc.dwhighdatetime; $ the //response every 1 seconds, remember that the third unit is subtle and easy to forget. theSetWaitableTimer (G_HTIMER,&LIUTC, +, null,null,false); the the 4) called in the thread function - DWORD WINAPI threadfunone (PVOID pvparam) in { the while(1) the { About WaitForSingleObject (g_htimer,infinite); theg_x++; thecout<<"I am Threadfunone:"<<g_x<<Endl; the } + return 0; - } the Bayi the the#include"windows.h" -#include"iostream" - using namespacestd; the LongG_x =0 ; the the //Define a Timer kernel object, and some variables about time the HANDLE G_htimer; - SYSTEMTIME St; the the FILETIME FTLOCAL,FTUTC; the 94 Large_integer LIUTC; the the //defining thread Functions 1 the DWORD WINAPI threadfunone (PVOID pvparam);98 About //Defining thread Functions 2 - DWORD WINAPI threadfuntwo (PVOID pvparam);101 102 intMain ()103 {104 the //Create a Timer kernel object that resets automatically106G_htimer =CreateWaitableTimer (null,false,null);107 108 //The first response was in 2014.08.10 at 17:12 minutes109 theSt.wyear = the ;111St.wmonth =8 ; the //St.wdayofweek =113St.wday =Ten ; theSt.whour = - ; theSt.wminute = A; theSt.wsecond =0 ;117St.wmilliseconds =0 ;118 119SystemTimeToFileTime (&st,&ftlocal); - 121LocalFileTimeToFileTime (&ftLocal,&FTUTC);122 123Liutc.lowpart =Ftutc.dwlowdatetime;124Liutc.highpart =Ftutc.dwhighdatetime; the 126SetWaitableTimer (G_HTIMER,&LIUTC, +, null,null,false);127 - //Create thread 1129HANDLE Hthreadone = CreateThread (NULL,0, Threadfunone,0,0, NULL); the CloseHandle (hthreadone);131 the //Create thread 2133HANDLE hthreadtwo = CreateThread (NULL,0, Threadfuntwo,0,0, NULL);134 CloseHandle (hthreadtwo);135 136 //let the main thread hang first to make sure that the other threads perform the completion137 138 GetChar ();139cout<<g_x<<Endl; $ return 0 ;141 }142 143 DWORD WINAPI threadfunone (PVOID pvparam)144 {145 while(1)146 {147 WaitForSingleObject (g_htimer,infinite);148g_x++;149cout<<"I am Threadfunone:"<<g_x<<Endl; Max }151 return 0; the }153 154 DWORD WINAPI threadfuntwo (PVOID pvparam)155 {156 while(1)157 {158 WaitForSingleObject (g_htimer,infinite);159g_x++; thecout<<"I am Threadfuntwo:"<<g_x<<Endl;161 }162 return 0;163}