MFC's timing function SetTimer and end KillTimer

Source: Internet
Author: User

When do we need to use the settimer function? You need to use the SetTimer function when you need to do one thing at a time . The method of using timers is simple, usually tells windows a time interval, and then windows periodically triggers the program at that interval. There are usually two ways to do this: sending wm_timer messages and invoking application-defined callback functions.


1.1 Using Wm_timer to set the timer

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

uint_ptr SetTimer (
  hwnd hwnd,               //  Window handle
  uint_ptr nidevent,      //  Timer id, multiple timers can be determined by the id to determine which timer
  uint uelapse,            //  interval
  timerproc lptimerfunc   //  callback function
);

For example
SetTimer (M_hwnd,1,1000,null); A timer that triggers once in 1 seconds
In an MFC program , SetTimer is encapsulated in a CWnd class, and the call does not have to specify a window handle.


So the prototype of the SetTimer function becomes:

UINT SetTimer (UINT nidevent,uint nelapse,void (CALLBACK EXPORT *lpfntimer) (Hwnd,uint, Yint, DWORD))


When you use the SetTimer function, a timer is generated. The nidevent in the function refers to the identifier of the timer, which is the name. Nelapse refers to the time interval, which is how often an event is triggered. The third parameter is a callback function, in which you put the code for the thing you want to do, you can set it to NULL, that is, using the system default callback function, the system defaults to the ontime function. How is this function generated? You need to generate the OnTime function in the class that needs the timer : in ClassWizard, select the class that needs the timer, add the wm_time message map, and automatically generate the OnTime function. Then add code to the function and let the code implement the function. It will be executed automatically every once in a while.


Cases:

SetTimer (1,1000,null);


1: The name of the timer;


1000: time interval, unit is milliseconds;


NULL: Use the ontime function.


Call KillTimer (nidevent) when no timer is required;


For example:killtimer (1);


1.2 Calling the callback function


This method first writes a callback function in the following format


void CALLBACK Timerproc (HWND hwnd,uint nmsg,uint ntimerid,dword dwtime);
Then use the SetTimer (1,100,timerproc) function to build a timer, the third parameter is the callback function address.


Perhaps you would ask, what if I want to add two or more two timer?


Continue with the SetTimer function, 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


}


}

Implementation of the MFC timer start /pause function

Recently encountered on the use of timers, on the internet also searched a lot of relevant information. All of them are talking about using SetTimer and KillTimer.

My project is based on a dialog box.

Add a timer to the OnInitDialog ().

BOOL Cmonitorprocessdlg::oninitdialog ()

{

SetTimer (1,3000,null); The first parameter can set itself as the ID of the timer , and the second parameter is the interval (in milliseconds)

}

Of course don't forget add of course don't forget add of course don't forget add wm_timer message OnTimer ()

And then I defined 2 global variables .

int npress = 0; Used to determine the pressed state of the Start button

BOOL bstatic = FALSE; Used to determine if the program is executing in order to KillTimer ()

For example, when the button is pressed, the

void Cmonitorprocessdlg::onbnclickedbuttonstart ()

{

MessageBox ("111111");

npress++; Bstatic = TRUE;

}

Add code inside the OnTimer () function.

void Cmonitorprocessdlg::ontimer (Uint_ptr nidevent)

{

TODO: Add Message Handler code and /or call default values here

if (nidevent ==1 && npress ==1)

{

Onbnclickedbuttonstart (); Npress--;

}

if (bstatic = = TRUE)

{

KillTimer (1);

}

Cdialog::ontimer (nidevent);

}

MFC's timing function SetTimer and end KillTimer

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.