Solved

AfxMessageBox victim of GUI message handlers

Posted on 1998-12-18
5
275 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for the 'C' grade ....
:)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
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…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now