Solved

AfxMessageBox victim of GUI message handlers

Posted on 1998-12-18
5
299 Views
Last Modified: 2013-12-03
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?
0
Comment
Question by:aderounm
  • 3
  • 2
5 Comments
 
LVL 6

Accepted Solution

by:
snoegler earned 190 total points
ID: 1417484
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( );
            break;
        }
    }
    // 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.
0
 

Author Comment

by:aderounm
ID: 1417485
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?
0
 
LVL 6

Expert Comment

by:snoegler
ID: 1417486
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().
0
 

Author Comment

by:aderounm
ID: 1417487
Thanks for the suggestion. CWnd::MessageBox is also associated with the GUI. However, MessageBox() with a NULL hwnd works.
0
 
LVL 6

Expert Comment

by:snoegler
ID: 1417488
Thanks for the 'C' grade ....
:)
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question