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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
bhatCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.