Usage of C + + timers: SetTimer and OnTimer

Source: Internet
Author: User

The use of the SetTimer function

1) Use Wm_timer to set the timer

First look at SetTimer, the prototype of this API function.

Uint_ptr SetTimer (HWND hwnd,//and timer associated Windows Uint_ptr nidevent,//a non-0 digital flag This timer UINT uelapse,//Specifies the time interval, in milliseconds Timerproc lptimerfunc//is generally specified as null);

When in the MFC project, the following:

Uint_ptr SetTimer (uint_ptr nidevent,//a non-0 digital flag This timer UINT uelapse,//the specified time interval, in milliseconds Timerproc lptimerfunc//is generally specified as null);

Use the following:

SetTimer (1,1000,null);

Parameters:

1: The name of the timer;

1000: time interval, unit is milliseconds;

NULL: Use the OnTime function.


The WM_TIMER message is triggered at each time to 1s, and its message response function is as follows:

Void cxxx::ontimer (uint_ptr nidevent)  {         //  TODO:  Add the message handler code here and/or call the default value                   //  Add the following code here:                  switch () {                  case 1:                             //doing what should be done                               //call KillTimer (1) Here when it is not needed;                             break;                  default:                            break;                  }      cdialog::ontimer (nidevent);  }

Call KillTimer (nidevent) when no timer is required;

KillTimer (1);


2) What if I want to join two or more than two timer?

Continue with the SetTimer function bar, the last timer ID is 1, this time can be 2,3,4 ....

SetTimer (2,1000,null); SetTimer (3,500,null);

Well, windows will coordinate with them. Of course, the OnTimer function body is also to be changed, to add each timer's processing code in the function body:

OnTimer (nidevent) {switch (nidevent) {Case 1: ...;        Break            Case 2: ...;        Break            Case 3: ...;    Break }}

Usage of C + + timers: SetTimer and OnTimer

Related Article

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.