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

Popular posts from this blog

Outlook : "operation failed, object could not be found. " when trying to close a PST.

How to transfer app and data from old iPhone to new iPhone for model IOS17 and IOS18 above.

Troubleshooting Outlook Express (IMAP, POP3, HTTP, NEWS) with the log file