Use settimer to perform periodic operations without a window

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.