Original link: http://www.educity.cn/develop/478947.html
VC provides a lot of time operation of the function, writing programs we can follow the timing of the different precision requirements to choose different time functions to complete the timing and timing operations.
Mode one: VC in the WM_TIMER message map can be a simple time control. First call the function SetTimer () to set the timing interval, such as SetTimer (0,200,null), which is the time interval for setting 200ms. The timer response function OnTimer () is added to the application, and a response processing statement in the function is used to complete the operation to arrive at the timed time. This timing method is very simple, can achieve a certain timing function, but its timing function as the sleep () function of the delay function, the accuracy is very low, the minimum timing accuracy is only 18ms. CPU consumption is low, and the timer message priority in the multitasking operating system is very low, can not be timely response, often can not meet the real-time control environment applications. Can only be used to achieve such as the dynamic display of bitmaps, such as the timing of the accuracy of the situation is not high.
Mode two: VC using the Sleep () function to achieve the delay, it is the unit of MS, such as delay of 2 seconds, with Sleep (2000). The accuracy is very low, the minimum timing accuracy is only 30ms, with the sleep function disadvantage lies in the delay period can not process other messages, if the time is too long, as if the crash, CPU occupancy rate is very high, can only be used in the request is not high latency program.
Method Three: Use the COleDateTime class and the COleDateTimeSpan class to combine the Windows message processing process to achieve the second-level delay. The following is a delay code that implements 2 seconds:
COleDateTime start_time = Coledatetime::getcurrenttime ();
COleDateTimeSpan end_time= coledatetime::getcurrenttime ()-start_time;
while (End_time. Gettotalseconds () < 2)//implementation delay 2 seconds
{
MSG msg;
GetMessage (&msg,null,0,0);
TranslateMessage (&MSG);
DispatchMessage (&MSG);
The above four lines are implementations that can process other messages during delay or timing.
Although this can reduce the CPU share,
However, the delay or timing accuracy is reduced, and can be removed in practical applications.
End_time = Coledatetime::getcurrenttime ()-start_time;
}//This allows us to handle other messages when the delay is over.
Mode four: In the case of high accuracy requirements, VC can take advantage of the GetTickCount () function, the return value of the function is a DWORD type, indicating the time interval after the computer starts in Ms. The accuracy is higher than the WM_TIMER message map, its timing error is 15ms in the shorter timing, its timing error is low in the longer timing, if the timing time is too long, as if the crash, CPU occupancy rate is very high, can only be used in the request is not high delay program. The following code enables precise timing of 50ms:
DWORD Dwstart = GetTickCount ();
DWORD dwend = Dwstart;
Do
{
Dwend = GetTickCount ()-Dwstart;
}while (Dwend <50);
To enable the GetTickCount () function to process other messages during delay or timing, you can change the code to:
DWORD Dwstart = GetTickCount ();
DWORD dwend = Dwstart;
Do
{
MSG msg;
GetMessage (&msg,null,0,0);
TranslateMessage (&MSG);
DispatchMessage (&MSG);
Dwend = GetTickCount ()-dwstart;
}while (Dwend <50);
Although this can reduce the CPU's share, and during the delay or timing can also handle other messages, but reduce the delay or timing accuracy.
Mode five: A multimedia timer function similar to the GetTickCount () function, DWORD timegettime (void), the function has a timing accuracy of MS, and returns the number of milliseconds that have elapsed since Windows started. Microsoft provides the underlying API for precise timers in its multimedia windows, using a multimedia timer to accurately read the current time of the system and to complete the invocation of an event, function, or procedure in a very precise time interval. The difference is that Winmm.lib and Mmsystem.h must be added to the project before the DWORD timeGetTime (void) function is called, otherwise the DWORD timegettime (void) function is not defined at compile time. Due to the use of this function is the method of timing control through the query, so, should establish a timing loop to control the timing events.
Mode VI: Using the multimedia timer timesetevent () function, the function has a timing accuracy of MS class. The function can be used to implement periodic function calls. The prototype of the function is as follows:
Mmresult timesetevent (UINT udelay,
UINT Uresolution,
Lptimecallback Lptimeproc,
WORD Dwuser,
UINT fuevent)
This function sets a timed callback event, which can be a one-time event or a recurring event. Once the event is activated, the specified callback function is invoked, the identifier code of the event is returned after success, otherwise NULL is returned. The parameters of the function are described as follows:
Udelay: Specifies the period of the event in milliseconds.
Uresolution: Specify the precision of the delay in milliseconds, the smaller the value the higher the timer event resolution. The default value is 1ms.
Lptimeproc: Point to a callback function.
Dwuser: Store user-provided callback data.
Fuevent: Specifies the Timer event type:
Only one event is generated after Time_oneshot:udelay milliseconds
Time_periodic: Generates events periodically every udelay millisecond.
For specific applications, you can complete the desired event by calling the timeSetEvent () function and defining the tasks that need to be executed periodically in the Lptimeproc callback function (e.g., timed sampling, control, etc.). It is important to note that the task processing time cannot be greater than the period interval. In addition, after the timer is used, the timekillevent () should be called in time to release it.
Method Seven: For more precise timing operations, you should use the QueryPerformanceFrequency () and QueryPerformanceCounter () functions. These are the exact time functions provided by the VC for use by Windows 95 and subsequent versions only, and require the computer to support precise timers from the hardware.
The QueryPerformanceFrequency () function and the QueryPerformanceCounter () function are prototyped as follows:
BOOL QueryPerformanceFrequency (Large_integer *lpfrequency);
BOOL QueryPerformanceCounter (Large_integer *lpcount);
The data type Arge_integer can be either a 8-byte integer or a union structure of two 4-byte-long integers, depending on whether the compiler supports 64-bit. This type is defined as follows:
typedef Union _LARGE_INTEGER
{
struct
{
DWORD LowPart;//4-byte integer number
LONG highpart;//4-byte integer number
};
Longlong QuadPart;//8-byte integer number
}large_integer;
Before timing, call the QueryPerformanceFrequency () function to get the clock frequency of the internal timer of the machine, and then call the QueryPerformanceCounter () function before and after the event that requires strict timing. The exact time of the event is calculated using the count difference and clock frequency obtained two times. The following code implements the exact timing of 1ms:
Large_integer litmp;
Longlong Qpart1,qpart2;
Double Dfminus, Dffreq, Dftim;
QueryPerformanceFrequency (&LITMP);
Dffreq = (double) litmp. quadpart;//get the clock frequency of the counter
QueryPerformanceCounter (&LITMP);
QPart1 = litmp. quadpart;//getting the initial value
Do
{
QueryPerformanceCounter (&LITMP);
QPart2 = litmp. quadpart;//Get Abort value
Dfminus = (double) (QPART2-QPART1);
Dftim = dfminus/dffreq;//Gets the corresponding time value in seconds
}while (dftim<0.001);
In addition, it is worth mentioning that, due to the difference between the machine itself and the machine running load different, to achieve very accurate timing is actually difficult to do, the above timing accuracy is theoretically achievable, the actual operation to reduce a lot. The seventh way to do experimental accuracy is basically accurate to 0.1 milliseconds, which is sufficient for the general procedure.
The delay function in VC + +