//************************************** **************************************** * ***** // This program uses two timer implementation methods, all are based on APIs, rather than MFC // 1. is to use the thread timed sleep method, start the thread threadproc, output in the thread, and then sleep 1000 ms /// 2. use the timer method to set a Ms timer (settimer), capture the message, call the callback function // timerproc, and output def in this function. Note, settimer can directly write the callback function address without capturing messages. // 3. This example is very detailed. // 4. Good luck. ////************************************ **************************************** * ******* # Include "stdafx. H "# include <windows. h> DWORD winapi threadproc (lpvoid lpparameter) // method 1 {static DWORD tick = 0; dword tmp = gettickcount (); While (1) {tick = gettickcount ()-TMP; // obtain the number of MS from start to present. tmp = gettickcount (); printf ("thread_proc: ABC % d, tickcount: % d \ n", tick, TMP ); // The interval of milliseconds between printing ABC and the last printing to the current printing. Sleep (1000); // print ABC} void timerproc () Every Ms // method 2 {static DWORD tick = 0; static dword tmp = gettickcount (); tick = gettickcount ()-TMP; TMP = gettickcount (); printf ("timerproc ____________ def % d \ n", tick);} int main () {DWORD dwthread ;:: createthread (null, 0, threadproc, (lpvoid), & dwthread); // generate a thread that outputs an "ABC" Every ms, and then sleepsettimer,, 0); // set a timer. The timer callback function is 0. Only the timer message is generated and the MSG is captured in the main function; Bool flag; while (1) // This loop captures timer messages and prevents the main function from exiting {peekmessage (& MSG, null, wm_user, wm_user, pm_noremove ); while (flag = getmessage (& MSG, null, 0, 0 ))! = 0) {If (flag =-1) {// handle the error and possibly exit} else if (MSG. message = wm_timer) {timerproc (); // if it is a timer message, call timerproc} else {translatemessage (& MSG); dispatchmessage (& MSG );}}}}