The usage of the timer in C ++,

Source: Internet
Author: User

The usage of the timer in C ++,

To put it bluntly, three functions are used:

SetTimer (20, 20, 0); // The first 20 indicates the identifier of the timer, the second 20 indicates the time you want to set, and the third does not need to worry, set 0.

Void CLMS511_interfaceDlg: OnTimer (UINT_PTR nIDEvent)
// TODO: Add your message handler code here and/or call default

If (nIDEvent = 20) // This 20 is the first 20.

DrawData ();
If (MySSocket. StopFlag)
SendServer ();

CDialog: OnTimer (nIDEvent );

KillTimer (20); // destroy the timer with the identifier of 20


The first parameter of the SetTimer () timer is the timer identifier. Any non-zero integer can be used. The second parameter is the timer interval, in milliseconds. The third parameter can be NULL,
In this case, WM_TIMER messages are added to the application's message queue and processed by the CWnd class object.
For example, the interval of SetTimer (, NULL) is 1 millisecond.
The following describes the types of scheduled functions:
Windows API
Parameter meaning:
1. Handle of the window where the notebook is located
2. Serial number
3. Time Period
4. Record the Response Function

CWnd class
CWnd: SetTimer
UINT SetTimer (
UINT nIDEvent, // The identifier of the timer
UINT nElapse, // The time to be scheduled
Void (callback export * lpfnTimer )(
The following is an example of how to test the running time of a program in C ++:
Unsigned long ticks1, ticks2;
Write at the beginning of the test
Ticks1 = GetTickCount ();
Write at the end of the test section
Ticks2 = GetTickCount ();

Ticks2-ticks1 is the time used
Add the header file Winbase. h.

Next we will introduce a method for measuring the execution time of a program:

<Time. h> Version


  1. Clock_t start = clock ();
  2. /* Code to be tested */
  3. Clock_t end = clock ();
  4. Cout <end-start <endl;

Most of us are familiar with this version. This should be the most common method for testing the execution time, which can be accurate to the ms level.


Function TRACE () that is printed to the screen when the program is running ()
TRACE ("distance: % d", m_DIST [I]); // output information in the output column.
Logs are recorded. % S is replaced with the address value during program execution. Replace % d with the error code returned by the WSAGetLastError () function.
The overall translation is Server "address" Can't Find Code = "WSAGetLastError ()"
Whether it is written to a file or output to the standard output screen depends on the implementation of TRACE.

There is also a scheduled method, which is implemented using the sleep () function. Call windows. h

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: 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.