How to Use the settimer () function in a thread environment without a window
Key point: The getmessage (dispatchmessage) must be in the same thread as the settimer function.
# Include <iostream> # Include <Stdio. h> # Include <Windows. h> # Define ID _ timer 100 Void Callback timerproc (hwnd, uint umsg, uint_ptr idevent, DWORD dwtime) {systemtime st; getlocaltime ( & St); printf ( " % 2D: % 2D: % 2D. % 3d hello! \ N " , St. whour, st. wminute, st. wsecond, st. wmilliseconds );} Int Main ( Int Argc, Char * Argv []) { Int Iret = settimer (null, idt_timer, 1000 , (Timerproc); MSG; While ( 1 ) {Getmessage ( & MSG, null, 0 ,0 ); Dispatchmessage ( & MSG );} Return 0 ;}
Generally, a thread system without a window does not specify a message queue for it. To stimulate a message queue, you can use postthreadmessage to send it to your own thread (getcurrentthreadid ()).
You can also create a short pattern loop by yourself. Using getmessage (...) can also stimulate the generation of message queues.
See:
Http://topic.csdn.net/t/20040607/14/3070354.html
http://hi.baidu.com/guangbinw/blog/item/b0818afbc7364760024f560a.html