Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

AfxMessageBox victim of GUI message handlers

Posted on 1998-12-18
5
Medium Priority
?
343 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 380 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…

963 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