Method One:
Create a thread and read the system time over and over again? If the timing requirements are not strict, with sleep can be.
DWORD WINAPI TimerThread (lpvoid pamaram) { UINT oldtickcount, Newtickcount; Oldtickcount = GetTickCount (); Gets the number of milliseconds while (true) { while (true) { newtickcount = GetTickCount (); Gets the number of milliseconds if (newtickcount-oldtickcount >= 60000) { oldtickcount = Newtickcount; break; } else switchtothread (); } 60 milliseconds Auto Call function somefun (); } return 0; }
Method Two: the thread function in the DLL can be used like this using the timerUINT ThreadProc (LPVOID) {SetTimer (null, 1, n, null); MSG msg;//PeekMessage forces the system to establish a message stack peekmessage (&msg, NULL, NULL, NULL, FALSE) for the thread, while (GetMessage (&msg, NULL, NULL, NULL)) {switch (msg.message) {case wm_timer:{//is executed once every 5 seconds}break;} TranslateMessage (&msg);//dispatchmessage (&msg);} KillTimer (NULL, 1); return 0;}
DWORD Tidtimer; UINT Uelapse; unsigned __stdcall timerthreadproc (void *param) { MSG msg; Uint_ptr Htimer; if (Htimer = SetTimer (null, NULL, UELAPSE, 0)) = = 0) MessageBox (null, L "Set Timer failed.", L "Error", MB_OK); while (GetMessage (&msg, NULL, 0, 0)) { switch (msg.message) {case Wm_timer: //... break; Default: Break ; } } if (Htimer) KillTimer (NULL, htimer); return 0;} _beginthreadex (null, 0, timerthreadproc, NULL, 0, &tidtimer); ... PostThreadMessage (Tidtimer, wm_timer, 0, 0); A subsequent event handler starts the timer
Implementation of timer function in MFC DLL