Link to home
Start Free TrialLog in
Avatar of rickatseasoft
rickatseasoft

asked on

Checking For Messages

I have an application that calls a background thread to do some processing.  I want the user to see that the thread is still processing, so my intention is to leave the orinal dialog on screen.  However, I want them to be able to minimize and/or restore the dialog.  Currently I am using

if(m_pThread){
   while(WaitForSingleObject(m_pThread->m_hThread,500)==WAIT_TIMEOUT);
}

Unfortunately, the window still can't receive the minimize message.  How can I suspend exectuion long enough for the message queue to be checked.

Another option is that I don't understand the problem at all, and there is something else completely wrong.

Thanks in advance, Rick
Avatar of mahesh1402
mahesh1402
Flag of India image

what processing you are doing in your thread function ? which kind of thread it is ??

MAHESH
Avatar of rickatseasoft
rickatseasoft

ASKER

The thread is a worker thread that is basically going through a series of files and crunching numbers to generate a report.

Rick
I wonder it PeekMessage() is the answer?
Perhaps I wasn't clear enough.

The user fills in a Dialog, and presses OK.  The OK and Cancel buttons are disabled, and the worker thread is called.  I am waiting for the worker thread to return before allowing the CDialog::OnOK() function to process.

Rick
If its user interface thread then the default implementation of CWinThread::Run() provides a message pump for your new thread. You can override this function to do anything else you want your thread to do, although most user-interface threads will simply use the default implementation of CWinThread::Run().

In addition, you may override the PreTranslateMessage() function if you want to intercept messages before the message pump dispatches them, although most messages can be handled in the message map.

MAHESH
so you are creating Window / Dialog in worker thread ????

MAHESH
Mahesh:

Thanks for getting back so quickly, but I still don't understand.  If the window is NOT processing messages while in the above loop (at least I think it is not), then how can I make that happen?  If the user clicks on minimize, the window will not respond until the thread returns.  I think that I need some way to check the message queue between iterations of the while loop.

Rick
I will suggest you refer this full article..

http://www.codeproject.com/threads/usingworkerthreads.asp

MAHESH
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Well 1st as i suggested refer above article FULLY.

Then :

For your needs worker thread is only solution ? if so You really need window in thread ( or it may be created and managed in main thread) ?

MAHESH

There is no need to use any threads. 'MsgWaitForMultipleObjects()' will allow message dispatching during wait operations.
Mahesh:

I ended up going back to my original WaitForSingleObject(), but adding your PeekMessage() suggestion.  It seems to work fine.  Thank You!

Do you see any problem with doihg it that way.

Rick
jkr:

I tried just placing the MsgWaitForMultipleObject(), and messages weren't processed.  At least the minimize, etc. were ignored.

But thanks for taking the time to comment.

Rick
Hm, that usually works just fine - at least, that snippet used to work fine for years ;o)
<The user fills in a Dialog, and presses OK.  The OK and Cancel buttons are disabled, and the worker thread is called.  I am waiting for the worker thread to return before allowing the CDialog::OnOK() function to process.>

The whole point of using a thread is to allow the app to continue independantly of the thread.  From what you say (halt dialog until thread completes) you *almost* certainly do not require a thread - it only makes everything more complex (and error prone).
Assuming the processing is being done in a loop you can use code like
while(processing)
{
....
DoEvents();
}


where DoEvents is code like
void DoEvents()
{  
      MSG msg;  
      while ( ::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE ) )
      {  
            ::TranslateMessage(&msg);
            ::DispatchMessage(&msg);
      }
}

This will allow your dialog to respond to minimise/restore/move whilst processing occurs.
Other events are continuing, other processing is continuing.  The whole purpose for leaving the dialog up is visual feedback to the user that a report is processing.  My problem is that there is a full screen background window that, when clicked, needs to minimize all other dialogs.  The dialog in question, refuses to minimize until its thread is finished.  Mahesh actually gave the answer that worked.

Thanks for commenting
JKR:

I misread above, and asked Mehesh a question that I should have asked you.  Do you see a problem with substituting WaitForSingleObject() in place of your suggested WaitForMultipleObjects(), and using your suggested PeekMessage()?

Rick
>>Do you see a problem with substituting WaitForSingleObject() in place of your suggested
>>WaitForMultipleObjects()

Well, it has to be 'MsgWaitForMultipleObjects()' - but I don't see any problems. This function will wait until either a message is received or the thread terminates. Combining that with a loop will dispatch the messages until the thread terminates, from then on the messages will be dispatched by the 'regular' message pump again.