Discussion on time function in VC

zhaozj2021-02-16  99

Regarding the business issues in the VC time function, there are many posts in the forum, and the discussion results and related knowledge are combined.

First talk from one how to delay in the program, the delay method has a method: use the SLEEP function, its minimum unit is 1ms, such as delay 2 seconds, with SLEEP (2000). Method 2: Using the unfavorable part of the SLEEP function is during the period, other messages cannot be handled, if time is too long, it is like a crash, so we use

The COleDateTime class and message handling process COleDateTimeSpan WINDOWS type of binding to implement delay: COleDateTime start_time = COleDateTime :: GetCurrentTime (); COleDateTimeSpan end_time = COleDateTime :: GetCurrentTime () - start_time; While (end_time.GetTotalSeconds () <= 2) / / Implement 2 seconds {msg? Msg; getMessage (& MSG, NULL, 0, 0); TranslateMessage (& MSG); DIT_TIME = COLEDATETIME :: getCurrentTime-start_time;} // This is when it is delayed We can also handle other messages. Method 3: The getTickCount () function can be used, the return value of the function is a DWORD type, indicating the time interval that experiences after the computer starts in milliseconds. DWORD DWSTART = GetTickCount (); DWORD DWEND = dwstart; do {msg? Msg ;? GetMessage (& MSG, NULL, 0, 0); TRANSLATEMESSAGE (& MSG); DSPATCHMESSAGE (& MSG); DWEND = GettickCount () ;?} While ((DWEND - DWSTART)? <=? 2000); The above method is in the precision of the delay, many times, we can't meet our requirements, below is a more accurate micro-second delay: large_integer bitmp; longlong qPart1 , QPart2; Double D = 0; QueryperFormanceCounter (& Litmp); // Get the initial value qPart1 = Litmp.quadpart; while (D <40) // What you want {QueryPerformanceCounter (& litmp); qpart2 = litmp.quadpart; D; D; D; D = (Double) (qPart2 - qPart1);

转载请注明原文地址:https://www.9cbs.com/read-10546.html

New Post(0)