Click to See Complete Forum and Search --> : Urgent: problems using OnIdle() function


June 1st, 1999, 07:24 AM
I am using the OnIdle function following the Help instructions. I use the override version in my App class like this:


BOOL CMyApp::OnIdle(LONG lCount)
{
if(CWinApp::OnIdle(lCount))
return TRUE;

if(!m_bReceptionOK ) {
if(GetTickCount() - m_dwInitOp > COMMTMOUT) {
TRACE("Timeout occured\n");
return FALSE;
}
return TRUE;
}

return FALSE;
}



I use this function to see if a timeout occured in a serial communication. But the verification is only executed when there are messages in the application queue, like mouse messages. If a put a TRACE after the CwinApp::OnIdle call I note that lCount is always 1.

If a timeout occurs the TRACE is executed only when I move the mouse, for example. What am I doing wrong? The problem is the GetTickCount() function?

Please help me soon.

Thanks,

Celso Aquino

June 1st, 1999, 09:24 AM
I'm not sure I'd use OnIdle() for communications timing...using an internal Timer would be much more reliable.

Cheers!
Humble Programmer
,,,^..^,,,

Josh Handley
June 1st, 1999, 10:37 AM
If you always return TRUE from OnIdle() then OnIdle will continue to be called. Once you return FALSE from OnIdle() then OnIdle() will stop being called until there is a mouse or keyboard event. To see how this works you can look at the source code for CWinThread()::Run() in THRDCORE.cpp:


// main running routine until thread exits
int CWinThread::Run()
{
ASSERT_VALID(this);

// for tracking the idle time state
BOOL bIdle = TRUE;
LONG lIdleCount = 0;

// acquire and dispatch messages until a WM_QUIT message is received.
for (;;)
{
// phase1: check to see if we can do idle work
while (bIdle &&
!::PeekMessage(&m_msgCur, NULL, NULL, NULL, PM_NOREMOVE))
{
// call OnIdle while in bIdle state
if (!OnIdle(lIdleCount++))
bIdle = FALSE; // assume "no idle" state
}

// phase2: pump messages while available
do
{
// pump message, but quit on WM_QUIT
if (!PumpMessage())
return ExitInstance();

// reset "no idle" state after pumping "normal" message
if (IsIdleMessage(&m_msgCur))
{
bIdle = TRUE;
lIdleCount = 0;
}

} while (::PeekMessage(&m_msgCur, NULL, NULL, NULL, PM_NOREMOVE));
}

ASSERT(FALSE); // not reachable
}




In section one once OnIdle returns false bIdle is set to false and is only set to true again if a there is a message in the queue that causes IsIdleMessage() to return true.

Josh