Solved

AfxMessageBox victim of GUI message handlers

Posted on 1998-12-18
5
314 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
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…

739 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