leowlf
asked on
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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 ..:(
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 ..:(
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_hPortHandl e, &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_cs CriticalSe ction);
//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_hPortHa ndle);
// release critical section LeaveCriticalSection(&g_cs CriticalSe ction);
}
default:
break;
}
}
AfxEndThread(0);
return TRUE;
}
Hope this might give u some idea.
Bhat
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_hPortHandl
//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_cs
//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_hPortHa
// release critical section LeaveCriticalSection(&g_cs
}
default:
break;
}
}
AfxEndThread(0);
return TRUE;
}
Hope this might give u some idea.
Bhat
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=-
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=-
Why u dont want to use Sleep()?. Is that does not satisfy Ur resolution?. Give some more details.
VinExpert