Solved

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

Posted on 2011-03-01
7
414 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:rjorge
7 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 35011513
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

0
 
LVL 16

Expert Comment

by:HooKooDooKu
ID: 35011577
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);
}

0
 
LVL 32

Accepted Solution

by:
sarabande earned 500 total points
ID: 35013395
i recommend against opening a second message queue handling cause you badly can prevent from getting trapped if the user for example closes the dialog while you currently were doing some work. instead move the lengthy work to a thread like hookoodooku has suggested:


void mydlg::OnButtonClickedLengthyWorkBegin()
{
    m_threadRunning = true;
     _beginthread(doLengthyWork, 0, this);
}

 doLengthyWork is declared a static member function of your dialog class:

// mydlg.h
...

class mydlg : public CDialog
{  
public:
   bool m_threadRunning; // init false in constructor
   bool m_threadStop; // init false in constructor

   static void doLengthyWork(void * p);

// put here your current members

};

// mydlg.cpp

...
void mydlg::doLengthyWork(void * p)
{
    mydlg * pthis = (mydlg*)p;   // now we got a pointer to the dialog

    // here put the code of work
    // you can communicate with main thread by pthis->PostMessage(....);

        // check for termination as often as  possible
        if (pthis->threadStop)
        {
            pthis->m_threadRunning = false;
            return;    // close thread
        }
   ....
   
   pthis->m_threadRunning = false;
}

note when the dialog is closed by the user you have to signal the thread that it should terminate

void mydlg::terminateThread()
{
      m_threadStop = true;
     while (m_threadRunning == true)
      {
           Sleep(10);
      }
}

void mydlg::OnOk()
{
     terminateThread();
     EndDialog(IDOK);
}

void mydlg::OnCancel()
{
     terminateThread();
     EndDialog(IDCANCEL);
}

Sara

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:rjorge
ID: 35022990
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?
0
 
LVL 32

Expert Comment

by:sarabande
ID: 35025011
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
0
 
LVL 11

Expert Comment

by:DeepuAbrahamK
ID: 35091427
Congratulations Sara for getting the Master Certification within a month !!
0
 
LVL 32

Expert Comment

by:sarabande
ID: 35092375
thank you guru deepu :)

Sara
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

746 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

12 Experts available now in Live!

Get 1:1 Help Now