VC function in the operation delay

  Talking about the delay in the process, think of how you will do, open a new thread?    If I am the only single-threaded process, function, but want only 10 seconds on the return value, but also can not be like the use of Sleep function can not be dealt with as other information? 

  I am here to see the Forum, the summing up of several delayed mode.    In addition, mainly the study of the sources of others, the copyright is not me, if this article useful to you, please thank respectively in the text of these authors (CSDN the ID): laiyiling (the most familiar strangers), QunKangLi (fog marks), tyzyx (Japan-bombing Island). 

  From strangers approach begins, this delay in the time span is the largest, at least in the second unit above: 
  Http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=195559 
  Seen more than one person has asked.    In fact, the estimated direct strangers handwritten this code, not from the copy of the procedures out of some mistaken hand, we will do their own adjustments to the # include 

  COleDateTime COleDateTime start_time =:: GetCurrentTime (); 
  COleDateTime COleDateTimeSpan end_time =:: GetCurrentTime () - start_time; 
  While (end_time.GetTotalSeconds () <= 2) 
  ( 
  MSG msg; 
  GetMessage (& msg, NULL, 0, 0); 
  TranslateMessage (& msg); 
  DispatchMessage (& msg); 
  COleDateTime end_time =:: GetCurrentTime () - start_time; 
  ) 
  I noted that the text of 
  PreTranslateMessage (& msg); 
  To replace: 
  TranslateMessage (& msg); 
  DispatchMessage (& msg); 
  Reason is that not only can be used in the MFC, but there are limitations PreTranslateMessage and thread information may result in obstruction. 
  Another point that because COleDateTimeSpan members of the function is also available in: 
  GetTotalMinutes, GetTotalHours, GetTotalDays to achieve greater time delay. 

  To a smaller time span, said that the implementation of millisecond delay on the use GetTickCount line: 
  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 microsecond delay: 
  LARGE_INTEGER litmp; 
  LONGLONG QPart1, QPart2; 
  Double d = 0; 
  QueryPerformanceCounter (& litmp); 
  / / Get initial value 
  QPart1 = litmp.QuadPart; 
  While (d <40) / / of the time you want to 
  ( 
  QueryPerformanceCounter (& litmp); 
  QPart2 = litmp.QuadPart; 
  D = (double) (QPart2 - QPart1); 
  ) 
  Source: http://community.csdn.net/Expert/TopicView1.asp?id=2663023.    Done to amend, if necessary microsecond delay also handle information, please refer to amend precedent. 

  Finally, if not met, then the delay clock cycles to do it: 

  # Define NOP_COUNT 3 / / in accordance with their own needs and LOOP NOP instructions cycle calculation. 
  __asm ( 
  MOV ECX, NOP_COUNT 
  DELAY: NOP 
  LOOP DELAY 
  ) 
  However, to do this work with VC is not a…… 

Bookmark it: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Google
  • DotNetKicks
  • DZone
  • Furl
  • Netvouz

Tags: ,

Releated Articles


0 Comments to “VC function in the operation delay”

No Comments. Send your comment.

Leave a Reply

You must be logged in to post a comment.