MFC Windows Message Processing - yield control
(Refering Programming Ms Visual C++ fifth edition - Ms Press)
A workaround which works with both Win16 and Win32. Yield control once in a while by inserting the following instructions inside the time consuming function:
MSG message;
if (::PeekMessage(&message, NULL, 0, 0, PM_REMOVE)) {
::TranslateMessage(&message);
::DispatchMessage(&message);
}
The PeekMessage function works like GetMessage, except that it returns immediately even if no message has arrived for your program. In that case, the function keeps on processing. If there is a message, however, the function pauses, the handler is called, and the function starts up again after the handler exits.
E.g.
void CMyClass::LengthyFunction()
{
MSG message;
volatile int nTemp;
for (m_nCount = 0; m_nCount < nMaxCount; m_nCount++) {
for (nTemp = 0; nTemp < 10000; nTemp++) {
// processing here.
}
if (::PeekMessage(&message, NULL, 0, 0, PM_REMOVE)) {
::TranslateMessage(&message);
::DispatchMessage(&message);
}
}
}
Comments