The delay operation in VC function

Source: Internet
Author: User

Speaking of the delay in the program, you will think of how to do, new open a thread? If my program uses only a single thread and wants to have the function wait 10 seconds to return the value, and not be able to handle other messages as it does with the sleep function?

I'm here to summarize some of the delays I can see in the forum. In addition, the main is the study of other people's sources, copyright is not in me, if this article for everyone, please thank the author of the article (CSDN ID): laiyiling (the most familiar with the Stranger), Qunkangli (fog marks), TYZYX (fried weekday island).

From the way a stranger handles it, this is the longest time span in a delay, at least in seconds:

http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=195559

I've seen more than one person ask. In fact, it is estimated that the stranger is directly handwritten this piece of code, not from the program section of the copy out, there are some hands wrong, everyone adjust it on the line

#include
COleDateTime start_time = COleDateTime::GetCurrentTime(); 
COleDateTimeSpan end_time = COleDateTime::GetCurrentTime() - start_time; 
while(end_time.GetTotalSeconds() <= 2) 

   MSG msg; 
   GetMessage(&msg,NULL,0,0); 
   TranslateMessage(&msg); 
   DispatchMessage(&msg); 
   end_time = COleDateTime::GetCurrentTime() - start_time; 
}

Notice what I've put in the original

PreTranslateMessage (&MSG);

Replace to:

TranslateMessage (&MSG);

DispatchMessage (&MSG);

The reason is that it can be used not only in MFC, but also in PreTranslateMessage, and may cause thread messages to block.

There is also a point, because the member functions of the COleDateTimeSpan class also include:

Gettotalminutes, Gettotalhours, gettotaldays, can achieve a larger time period delay.

To a smaller time span, the execution of millisecond-level delays is done with GetTickCount:

DWORD dwStart = GetTickCount();
DWORD dwEnd = dwStart;
do

   MSG msg; 
   GetMessage(&msg,NULL,0,0); 
   TranslateMessage(&msg); 
   DispatchMessage(&msg); 
   dwEnd = GetTickCount(); 
} while((dwEnd - dwStart) <= 2000);

Then the microsecond-level delay:

LARGE_INTEGER litmp ;
LONGLONG QPart1,QPart2 ;
double d=0;
QueryPerformanceCounter(&litmp) ;
// 获得初始值
QPart1 = litmp.QuadPart ;
while (d<40)//你想要的时间
{
   QueryPerformanceCounter(&litmp) ;
   QPart2 = litmp.QuadPart ;
   d=(double)(QPart2 - QPart1);
}

Source: http://community.csdn.net/Expert/TopicView1.asp?id=2663023. Do not make changes, if you need a microsecond delay in the processing of messages, please refer to the previous revision.

Finally, if not enough, then do the clock cycle delay bar:

#define Nop_count 3//needs to calculate itself according to the NOP and loop instruction cycles.

__asm {
  MOV ECX, NOP_COUNT
DELAY: NOP
  LOOP DELAY
}

However, the use of VC to do this job is not a bit ...

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.