AfxMessageBox victim of GUI message handlers

I have a code section that indicates the presence of an error with a call to AfxMessageBox(). Now, a message handler function for a button control on the GUI takes a long time (up to two minutes) to return during which the entire GUI is inactive. None of the GUI controls function until the message handler returns. Whenever an error occurs in the program the AfxMessageBox does not show until the message handler returns. Is it possible to make the AfxMessageBox independent of the GUI? Also is it possible to make the GUI active even while one message handler is being executed? How?
Who is Participating?
snoeglerConnect With a Mentor Commented:
To achieve what you want you need to run this computing-intensive process in a seperate
thread. The main thread, i.e. the one your application uses by default, handles both GUI
and computing. So if a long operation should be performed, the GUI blocks until the operation
is finished unless you don't create a seperate thread.
There is another method, but it isn't 'clean':

while ( bDoingBackgroundProcessing )
    while ( ::PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE ) )
        if ( !PumpMessage( ) )
            bDoingBackgroundProcessing = FALSE;
            ::PostQuitMessage( );
    // let MFC do its idle processing
    LONG lIdle = 0;
    while ( AfxGetApp()->OnIdle(lIdle++ ) )
    // Perform some background processing here
    // using another call to OnIdle

(Taken from the MSVC online help titled 'FAQ: Background Processing in an MFC App.'.

But i think the better and 'cleaner' method is to create a seperate thread.
There are two ways to do this:

a) Derive a class from CWinThread if you need some UI (windows, ...) within your thread
b) Create a simple worker thread like this:

UINT MyWorkerThread(LPVOID lpvData)
  // Do your operations here
  return 0;

// Within your message handler
  LPVOID lpvData= // some data you want to pass to the worker thread
  AfxBeginThread( (AFX_THREADPROC)MyWorkerThread, lpvData);

To get a notification if your worker thread has terminated, you can use either a CEvent or
send a message to a window using the message API.
aderounmAuthor Commented:
Actually the problem code section is already in a separate thread. When an error occurs it invokes an AfxMessageBox() routine. The AfxMessageBox() does not show up for a long time if the long GUI message handler is already being executed. I need a way of telling to GUI to handle the AfxMessageBox and then go back and continue processing the long message handler. It might help to place your ::PeekMessage code in the message handler?
AfxMessageBox() invokes CWinApp::DoMessageBox() - and as the CWinApp object is
located in the main thread, this will cause the blocking, i think.
So maybe your problem will be solved if you'd use CWnd::MessageBox().
aderounmAuthor Commented:
Thanks for the suggestion. CWnd::MessageBox is also associated with the GUI. However, MessageBox() with a NULL hwnd works.
Thanks for the 'C' grade ....
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.