1. If you do game development, in order to write a single thread for time is not a good deal, Createwaitabletimera and the like is a kernel object, because multiple clocks have the same name, you can have multiple threads simultaneously read a clock.
2. If you want to use more than one timer, you should set the name of each timer, if it is anonymous, it can only be one.
#include <stdio.h>#include<stdlib.h>#include<Windows.h>intMain () {HANDLE time1= Createwaitabletimera (NULL, TRUE,"Haihua"); if(time1==NULL) {printf ("creation failed"); } Large_integer MyTime; //Large integer, which is essentially a structural body mytime. QuadPart= -50000000;//The unit is 0.1 microseconds, set to 5 seconds after the timer is started.
if(! SetWaitableTimer (Time1,&mytime,0,0,0,0) {printf ("Setup failed"); } if(WaitForSingleObject (time1,infinite) = =wait_object_0) { //if equals wait_object_0, it means getting the message. printf ("wait OK"); } Else{printf ("Wait No"); } System ("Pause");}
The time timer also allows the callback function
#include <stdio.h>#include<stdlib.h>#include<Windows.h>
//function pointer as a parameter, the function he calls is the callback function.
VOID CALLBACK Timerun (void*Parg,dword Timearg,dword Timehigh) {DWORD dwindex= * (DWORD *) parg; printf ("\ n%d times .", dwindex); //messageboxa (0, "1", "2", 0);}intMain () {HANDLE time1= Createwaitabletimera (NULL, TRUE,"Haihua");//Create always if(Time1 = =NULL) {printf ("creation failed"); } Large_integer MyTime; MyTime. QuadPart= -50000000;//0.1 Subtle,DWORD dwparam=1;//Set Timer if(SetWaitableTimer (time1, &mytime, the, Timerun, &dwparam, FALSE)){ //3000, which represents 3000 milliseconds to perform a loop. printf ("wait 5 seconds to start working ."); for(inti =0; I < the; i++, dwparam++)//loops how many times{SleepEx (INFINITE, TRUE); }} cancelwaitabletimer (time1);//Cancel TimerCloseHandle (time1); System ("Pause");}
Time synchronization for multiple threads