Settimer timer usage

Source: Internet
Author: User

Link: http://blog.csdn.net/jiahehao/archive/2007/10/22/1837857.aspx

When do we need the settimer function? You need to use the settimer function to execute one thing at a time. The timer method is relatively simple. It usually tells windows a time interval, and then Windows triggers programs cyclically at this time interval. There are two methods to achieve this: sending the wm_timer message and calling the callback function defined by the application.

 

1.1 Use wm_timer to set the timer

First, see the prototype of the settimer API function.

Uint_ptr settimer (
Hwnd, // window handle
Uint_ptr nidevent, // timer ID, multiple fixed

// The timer ID can be used to determine which timer is used.
Uint uelapse, // time interval, in milliseconds
Timerproc lptimerfunc // callback function
);

For example
Settimer (m_hwnd, null); // a one-second timer
In the MFC program, settimer is encapsulated in the cwnd class, so you do not need to specify a window handle when calling it.

The prototype of the settimer function is:

Uint settimer (uint nidevent, uint nelapse, void (callback Export * lpfntimer) (hwnd, uint, yint, DWORD ))

When the settimer function is used, a timer is generated. In the function, nidevent refers to the timer identifier, that is, the name. Nelapse refers to the time interval, that is, the interval at which an event is triggered. The third parameter is a callback function. In this function, you can set the code of the thing you want to do to null, that is, use the default callback function of the system, the ontime function is recognized by default. How is this function generated? You need to generate the ontime function in the class that requires the Timer: In classwizard, select the class that requires the timer and add the wm_time message ing to automatically generate the ontime function. Then add the code to the function to implement the function. It is automatically executed every other time.

Example:

Settimer (1,1000, null );

1: The timer name;

1000: time interval, in milliseconds;

Null: Use the ontime function.

Call killtimer (nidevent) When no timer is required );

Example: killtimer (1 );

1.2 call the callback function

This method first writes a callback function in the following format

Void callback timerproc (hwnd, uint nmsg, uint ntimerid, DWORD dwtime );
Then use the settimer (1,100, timerproc) function to create a timer. The third parameter is the callback function address.

2. You may ask, what if I want to add two or more timers?

Continue to use the settimer function. The last timer ID is 1. This time it can be 2, 3, 4 ....

Settimer (2,1000, null );

Settimer (3,500, null );

Well, Windows will coordinate them. Of course, the ontimer function body also needs to change. You need to add the processing code of each timer in the function body:

Ontimer (nidevent)

{

Switch (nidevent)

{

Case 1 :........;

Break;

Case 2 :.......;

Break;

Case 3 :......;

Break;

}

}

When do we need the settimer function? You need to use the settimer function to execute one thing at a time. The timer method is relatively simple. It usually tells windows a time interval, and then Windows triggers programs cyclically at this time interval. There are two methods to achieve this: sending the wm_timer message and calling the callback function defined by the application.

 

1.1 Use wm_timer to set the timer

First, see the prototype of the settimer API function.

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
);

For example
Settimer (m_hwnd, null); // a one-second timer
In the MFC program, settimer is encapsulated in the cwnd class, so you do not need to specify a window handle when calling it.

The prototype of the settimer function is:

Uint settimer (uint nidevent, uint nelapse, void (callback Export * lpfntimer) (hwnd, uint, yint, DWORD ))

When the settimer function is used, a timer is generated. In the function, nidevent refers to the timer identifier, that is, the name. Nelapse refers to the time interval, that is, the interval at which an event is triggered. The third parameter is a callback function. In this function, you can set the code of the thing you want to do to null, that is, use the default callback function of the system, the ontime function is recognized by default. How is this function generated? You need to generate the ontime function in the class that requires the Timer: In classwizard, select the class that requires the timer and add the wm_time message ing to automatically generate the ontime function. Then add the code to the function to implement the function. It is automatically executed every other time.

Example:

Settimer (1,1000, null );

1: The timer name;

1000: time interval, in milliseconds;

Null: Use the ontime function.

Call killtimer (nidevent) When no timer is required );

Example: killtimer (1 );

1.2 call the callback function

This method first writes a callback function in the following format

Void callback timerproc (hwnd, uint nmsg, uint ntimerid, DWORD dwtime );
Then use the settimer (1,100, timerproc) function to create a timer. The third parameter is the callback function address.

2. You may ask, what if I want to add two or more timers?

Continue to use the settimer function. The last timer ID is 1. This time it can be 2, 3, 4 ....

Settimer (2,1000, null );

Settimer (3,500, null );

Well, Windows will coordinate them. Of course, the ontimer function body also needs to change. You need to add the processing code of each timer in the function body:

Ontimer (nidevent)

{

Switch (nidevent)

{

Case 1 :........;

Break;

Case 2 :.......;

Break;

Case 3 :......;

Break;

}

}

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.