How to implement 'Delay'?

VC6, WinNT/95/98

I have a single-thread application that communicate with some hardware. I need to have a delay after writing some data to the comm port before i read the data returned by the hardware.

Now i am using the sleep() command which i don't like.

I was thinking using something like :

void MySleep( int millisecond )
{
  while (true)
  {
    CTime t1 = GetCurrentTime();
    PeekMessage, translate, dispatch ... etc.
    CTime t2 = GetCurrentTime();
   
    CTimeSpan sp = t2-t1;
    if (sp.GetTotalSecond() > millisecond / 1000)
       return;
  }
}

Is there better implementation than this?  ... btw, and I cannot use the WM_TIMER.

Sample code appreciated.









 
leowlfAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
bhatConnect With a Mentor Commented:
Hi

I think U can do this :

DWORD  curtick = GetTickCount();

while(GetTickCount() < curtick+19)
{
    PeekMessage, translate, dispatch ... etc.  
}

I think  instead of 19 U can U can use the value U are giving in Ur Sleep() statement


Hope this takes care of Ur problem

Bhat
0
 
Vinayak KumbarSr Program ManagerCommented:
Hi,

Why u dont want to use Sleep()?. Is that does not satisfy Ur resolution?. Give some more details.

VinExpert
0
 
leowlfAuthor Commented:
Hi VinExpert,
under some situation, the delay need to be up to 3 minutes for the hardware to complete its task. I cannot have my application sleep() for 3 minutes ..:(

   
0
 
bhatCommented:
Hi,
Are u using a thread to monitor the comm Port ?
If Yes do the following in the monitoring thread.

UINT MonitorCOMPort(LPVOID pParam )
{
DWORD event;                  //For Event
OVERLAPPED overLapped;      //Overlapped Structure

//Create The Event
overLapped.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);

//Thread Loop Starts
while(1)
{
//Wait For The COM Event
 WaitCommEvent(g_hPortHandle, &event, NULL);

//If Rx Then Display In List
      switch(event)
      {
      case EV_RXCHAR:
            {
            unsigned long ii = 0;
            char chTemp[100];
            ZeroMemory(chTemp, 100);
//initialize the critical section where u are connecting to the h/w
//Enter The Critical Section                             EnterCriticalSection(&g_csCriticalSection);

//Read From Port
      ReadFile(g_hPortHandle, chTemp, 50, &ii, NULL);
//chTemp will have the data in the port

PurgeComm(g_hPortHandle, PURGE_RXABORT | PURGE_TXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR );
FlushFileBuffers(g_hPortHandle);

      // release critical section                  LeaveCriticalSection(&g_csCriticalSection);

                                          }
default:
break;
}            
}
      AfxEndThread(0);
      return TRUE;
}

Hope this might give u some idea.

Bhat
0
 
prangelCommented:
Hello leowlf,

If I understand your question correctly, you want to allow your app to continue to handle WM_PAINT messages and such while it is waiting for your hardware to get back to you. If so, try calling this little bit of code from inside your own MySleep(), 'while(true){}' loop.


void CSomeClass::CheckMsgQueue(){

MSG msg;

  while ( ::PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE ) )
    {
      if ( !AfxGetApp()->PumpMessage( ) )
      {
        break;
      }

    } // End of while()


} // End of CSomeClass::CheckMsgQueue(){

'Hope this helps"

-=lp=-
0
All Courses

From novice to tech pro — start learning today.