Click to See Complete Forum and Search --> : Simple question
Aslocal
April 8th, 1999, 07:12 PM
Hello. I was wondering if anybody could help me with a little problem. If I go into a loop in a message handler (like a click event for a button), then windows will no longer recognize any events on that window from my mouse or keyboard (ie. the window will be frozen until the loop completes).
Anyways, i was wondering if there was any way to make it so that the window will still receive events, while it is in the loop? Like maybe a function that allows the processing of uncompleted events, or something?
Any help would be appreciated!
Thanks alot, Marshall
April 8th, 1999, 08:44 PM
Your window isn't frozen. It's continuing to receive messages but they don't get processed until your handler returns. This is how Windows works. There's an "event loop" going on behind the scenes responding to all activity (mouse clicks, keyboard, etc.) which calls your handlers accordingly. Until each handler returns, those messages don't get processed. They get queued up until then. This is what's know as "event driven" processing. In any event, there are some unorthodox methods for doing what you want but they violate the recommended design strategy. On the bright side, this is a multi-tasking environment. So what you really want to do is start a separate thread to handle any long background tasks (such as the one your now using). Check out "AfxBeginThread()" and report back if you have any questions. One word of caution. Great care needs to be taken when running multiple threads. This is a deep subject however and I can't do justice to it here. You need to read up on the subject. "AfxBeginThread()" is easy to use however and should take relatively little time to learn. Just be cautious. If two threads (which you'll soon learn about) both access some global resource (a global variable perhaps, a file, etc.) you need to synchronize access to those resources. Semaphores, critical sections and so forth are some of the standard techniques for doing this. You'll need to read up on multitasking techniques to learn about them. Good luck.
LALeonard
April 9th, 1999, 10:26 AM
Sure, use this function ("Generic" is just a class of my - use your own name):
// Spin The Message Loop: C++ version. See "Advanced Windows Programming",
// M. Heller, p. 153, and the MS TechNet CD, PSS ID Number: Q99999.
/* static */ UINT Generic::SpinTheMessageLoop()
{
//TRACE(" *** Spinning the message loop\n");
MSG msg;
::ZeroMemory(&msg, sizeof(msg));
while (::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
// Include the WM_PAINT clause to inhibit redrawing.
if (WM_QUIT == msg.message) {
::PostQuitMessage(msg.wParam);
break;
} /* else if (WM_PAINT == msg.message) {
break;
} */ else if (! AfxGetApp()->PreTranslateMessage(&msg)) {
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
}
// Update user interface, then free temporary objects.
AfxGetApp()->OnIdle(0);
AfxGetApp()->OnIdle(1);
return msg.message;
}
You would call it like this:
void MyClass::OnButtonClick()
{
while ( doing stuff ) {
Do Stuff A
Generic::SpinTheMessageLoop();
Do Stuff B
}
}
The biggest danger with this is that the user can close your window or the whole app out from under you, so a good thing to do is this:
if (WM_QUIT == Generic::SpinTheMessageLoop()) {
YikesHandleExit();
}
HTH.
LA Leonard - Definitive Solutions, Inc.
Harvey Hawes
April 9th, 1999, 12:27 PM
Hi,
Depending on what you are doing in the handler, you might want to try a worker thread. Just create the thread in your message handler. The worker thread is really just a function that you call that "runs in the background" regardless of what the main thread is doing. Use ::PostMessage to update the view from the thread, and a CEvent from the view to stop the thread (or just let the function fall out of scope).
HTH,
Harvey
Harvey Hawes
Software Engineer
BioScience Analysis Software Ltd.
Masters Candidate
Cardiovascular/Respiratory Sciences
Faculty of Medicine
University of Calgary
Calgary, Alberta, Canada
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.