Link to home
Start Free TrialLog in
Avatar of rjorge
rjorge

asked on

How can I have a modal dialog box responsive while application is busy

Hi,
I'm working on a MFC application that is iterates performs some intensive work. In order to provide some feedback on the current progress of the processing I have a small CDialog
 derived class that I comunicate to it with messages.  In order to update the contentI send it a message like this. Inside the class I have the handling for this message.
m_pProgessDlg->SendMessage(WM_USER +5000, nCurrentCount, nTotall);

Open in new window

The handling class contains a pointer to the object of this class that is allocated like this:
m_pProgressDlg = new CProgressDlg;
  m_pProgressDlg->Create( CProgressDlg::IDD);

Open in new window


The problem I have is that when the executable is very busy the window of this CProgress instance seems not to be very instance. In fact clicking it shows the message "Window does not react".
 How can I make it stay responsive to UI events?

Cheers,
r

Avatar of jkr
jkr
Flag of Germany image

In which way is your app "very busy"? If you are performing some processing in a loop, you can keep the UI responsive by draining the message queue every once in a while, e.g.
void DoEvents ()
{

  while   (   PeekMessage (   &msg,   NULL,   0,  0,  PM_REMOVE))
  {
       DispatchMessage     (   &msg);
  }
}

for (;;)
{

   // a 'busy', time consuming loop
   // ...
   // ...
   // ...

   DoEvents(); // keep the UI happy
  
}

Open in new window

Avatar of HooKooDooKu
HooKooDooKu

Two thoughts come to mind:

1. Run the "intensive work" and the UI Dialog is seperate threads (requires a little effort to learn how to do multi-threaded applications if you don't already know).

2. Include some logic within the "intensive work" that occationally takes a "time-out" and checks the message que for any messages that need to be processed.  The following will process a single message (you might need to set up a loop to process multiple messages)

MSG msg;
if( ::PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE) )
{
    ::TranslateMessage(&msg);
    ::DispatchMessage(&msg);
}

ASKER CERTIFIED SOLUTION
Avatar of sarabande
sarabande
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rjorge

ASKER

Thanks Sara,
I-ve tried teh code but I have a problem. Inside the lengthy function I access a database by opning a CRecorset object. When I perform this from within the thread th e open never returns. Do you have a suggestion?
you can use a second CDataBase * pointer in your thread where you made a new connection to the db. the pointer would be passed into the constructor of your recordset class derived from CRecordset and makes the access from thread safe. you never should call functions of your dialog/view/recordset/application/document classes directly from thread. instead call PostMessage and sent a private message from worker thread to main thread which you can catch via message map.

Sara
Congratulations Sara for getting the Master Certification within a month !!
thank you guru deepu :)

Sara