Settimer is used to set the timer and execute an operation at intervals. The prototype is as follows:
Uint_ptr settimer (
Hwnd, // window handle
Uint_ptr nidevent, // timer ID. When multiple timers exist, you can use this ID to determine which timer is used.
Uint uelapse, // time interval, in milliseconds
Timerproc lptimerfunc // callback function
);
It triggers the callback function by distributing the wm_timer message. See the following code:
[CPP]View plaincopy
- Void callback timerproc (hwnd, uint nmsg, uint ntimerid, DWORD dwtime)
- {
- Printf ("% s", "ABC ");
- }
- Void main ()
- {
- Settimer (0, 0, 1000, & timerproc );
- }
Do you think the above Code will be correctly executed? The answer is no. The callback function cannot be executed at all. Although settimer is used, wm_timer messages are not distributed, so the callback function is not triggered.
[CPP]View plaincopy
- Void callback timerproc (hwnd, uint nmsg, uint ntimerid, DWORD dwtime)
- {
- Printf ("% s", "ABC ");
- }
- Void main ()
- {
- Settimer (0, 0, 1000, & timerproc );
- MSG;
- While (getmessage (& MSG, null, 0, 0 ))
- {
- If (msg. Message = wm_timer)
- {
- Dispatchmessage (& MSG );
- }
- }
- }
OK. Do you see the WHILE LOOP above? Here we get the wm_timer message sent every second and distribute it to notify the callback function to start execution.
Reference: http://blog.csdn.net/bdmh/article/details/6371443
Tested and feasible, complete code:
#include "stdafx.h"#include "windows.h"#include "stdio.h"void CALLBACK TimerProc(HWND hWnd,UINT nMsg,UINT nTimerid,DWORD dwTime){ printf("%s","abc"); }void main(){ SetTimer(0, 0, 1000, &TimerProc); MSG msg; while(GetMessage(&msg,NULL,0,0)) { if(msg.message==WM_TIMER) { DispatchMessage(&msg); } } }
Write the following statement:
MSG msg;GetMessage(&msg, NULL, 0, 0);
In this way, the message queue is available, but no one can send messages separately, so the timerproc content will not be executed.
Bytes ----------------------------------------------------------------------------------------------
Delphi version:
Notifications for using settimer in the console