Notifications for using settimer in the console

Source: Internet
Author: User

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
  1. Void callback timerproc (hwnd, uint nmsg, uint ntimerid, DWORD dwtime)
  2. {
  3. Printf ("% s", "ABC ");
  4. }
  5. Void main ()
  6. {
  7. Settimer (0, 0, 1000, & timerproc );
  8. }

 

 

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
  1. Void callback timerproc (hwnd, uint nmsg, uint ntimerid, DWORD dwtime)
  2. {
  3. Printf ("% s", "ABC ");
  4. }
  5. Void main ()
  6. {
  7. Settimer (0, 0, 1000, & timerproc );
  8. MSG;
  9. While (getmessage (& MSG, null, 0, 0 ))
  10. {
  11. If (msg. Message = wm_timer)
  12. {
  13. Dispatchmessage (& MSG );
  14. }
  15. }
  16. }

 

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

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.