Practical VC time control functions

Source: Internet
Author: User
Practical VC time control functions

2000-08-14 15:27:40

With the rapid development of software and hardware, computer technology has been widely used in the field of automation control, in order to achieve real-time control, the control program must be able to accurately complete the timing and timing functions. VC provides a lot of time-related functions, which are described in the following based on their precision.

General time control functions

VC programmers will use Windows WM-TIMER message ing for simple time control: 1. call the settimer () function to set the time interval. For example, settimer (0,200, null) sets the time interval of 200 milliseconds. 2. add the scheduled response function ontimer () to the application, and add the Response Processing statement to the function to complete the operation at that time. This timing method is very simple, but its timing function is the same as the delay function of the sleep () function, with low precision, this method can only be used to achieve situations where the timing precision is not high, such as dynamic display of Bitmap. However, this method should be avoided when the precision is high.

Precision Time Control Function

If the error is not greater than 1 millisecond, you can use the gettickcount () function. The return value of this function is DWORD, indicating the time interval after the computer starts in milliseconds. The following programming statement can be used to implement accurate timing within 50 milliseconds, with an error less than 1 millisecond.

DWORD dwstart, dwstop;

// Start value and end value

Dwstop = gettickcount ();

While (true)

{

Dwstart = dwstop;

// The Last termination value is changed to a new start value.

// Add the corresponding control statement here

Do

{

Dwstop = gettickcount ();

} While (dwstop-50 <dwstart );

}

High-precision time control functions

For general real-time control, the use of the gettickcount () function can meet the precision requirements, but to further improve the timing accuracy, we need to use the queryperformancefrequency () function and queryperformancecounter () function. These two functions are high-precision time functions provided by VC for Windows 9x only, and require computers to support high-precision timers from hardware. The prototype of the queryperformancefrequency () function and queryperformancecounter () function is:

Bool queryperformancefrequency (large-integer * lpfrequency );

Bool queryperformancecounter (large-integer * lpcount );

The Data Type Large-integer can be an integer that is 8 bytes long or a combination of two four-byte long integers, the specific usage depends on whether the compiler supports 64-bit. This type is defined as follows:

Typedef Union-large-integer

{

Struct

{

DWORD lowpart; // 4-byte integer

Long highpart; // 4-byte integer

};

Longlong quadpart;

// 8-byte integer

} Large-integer;

Before timing, call the queryperformancefrequency () function to obtain the clock frequency of the machine's Internal timer. I use this function on three Pentium ⅱ machines with clock speed of 266, 300, and 333. The clock frequency obtained is 1193180Hz. Next, I call the queryperformancecounter () function before and after events that require strict timing, and use the difference in counting and clock frequency obtained twice, you can calculate the exact time of the event experience. The following procedure is used to test the exact duration of the function sleep (100.

Large-integer litmp;

Longlong qpart1, qpart2;

Double dfminus, dffreq, dftim;

Queryperformancefrequency (& litmp );

// Obtain the counter clock frequency

Dffreq = (double) litmp. quadpart;

Queryperformancecounter (& litmp );

// Obtain the Initial Value

Qpart1 = litmp. quadpart;

Sleep (100 );

Queryperformancecounter (& litmp );

// Obtain the end value

Qpart2 = litmp. quadpart;

Dfminus = (double) (qpart2-qpart1 );

Dftim = dfminus/dffreq;

// Obtain the corresponding time value

Run the above program and the result is dftim = 0.097143767076216 (seconds ). Careful readers will find that the results of each execution are different and there are some differences, which is caused by the sleep () error.

This article introduces three implementation methods of timing or timing. You can choose based on your actual situation to implement the Program's timing and timing functions. The above programs are all debugged in the VC 6.0 and Windows 98 environments.

From SCID sun hequan/Wen

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.