For Windows under WinCEProgramYou can use API settimer to create a timer. When the timer times out, the window will receive a wm_timer message. We can do the timer time-out processing in the wm_timer message. For windowless programs, because there is no message loop, they cannot receive the wm_timer message, and the timer will not work properly.
So how does a windowless program use settimer for timing? In fact, we only need to add a message loop to the main thread in the windowless program to process wm_timer. For more information, seeCodeAs follows:
Uint utimerid =: settimer (null, null, 1000, null); <br/> MSG; <br/> // message loop <br/> while (getmessage (& MSG, null, 0, 0) <br/>{< br/> switch (MSG. message) <br/>{< br/> case wm_quit: <br/>: killtimer (null, utimerid); <br/> break; <br/> case wm_timer: <br/> // todo: Treat wm_timer message <br/> Break <br/> default: <br/> break; <br/>}< br/>}
However, it is still troublesome for windowless programs. When using a timer, you must add the message loop code. In fact, Microsoft also provides an API timesetevent to create a timer, for more precise timing, see the following code:
// Header file and library to be introduced <br/> # include <mmsytem. h> <br/> # pragma comment (Lib, "mmtimer. lib ") <br/> // timer callback function <br/> void callback timerproc (uint wtimerid, uint MSG, DWORD dwuser, DWORD dw1, DWORD dw2 ); <br/> // create a timer <br/> mmresult utimerid = timesetevent (, 1, timerproc, (DWORD) This, time_periodic | time_callback_function ); </P> <p> // timer timeout processing <br/> void callback timerproc (uint wtimerid, uint MSG, DWORD dwuser, DWORD dw1, DWORD dw2) <br/>{< br/> // todo: Place code here <br/>}
Do not forget to call timekillevent to disable the timer.