[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Problem with immediate termination of a dialog box?

Posted on 2005-03-01
8
Medium Priority
?
281 Views
Last Modified: 2013-11-20
Hi Experts

I have an MFC SDI application. I am having problem with the immediate termination of a dialog box in my application.

On making appropriate selection from the dialog box i have to run a long process so the cursor gets the hour glass shape & a progress bar runs in the status bar but before all this the dialog box shud terminate.

I am using the EndDialog() to terminate the dialog box but it is not happening. The dialog box is there as long as the long process is running.

How do i solve this problem.

Also from MSDN i found out that:

"EndDialog does not close the dialog box immediately. Instead, it sets a flag that directs the dialog box to close as soon as the current message handler returns."

I want to close the dialog box immediately.

waiting for suggestions

Regards


   
   
0
Comment
Question by:mathu76
8 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 300 total points
ID: 13436879
Write function DoEvents:

void CGeneralFunctions::DoEvents()
{
    MSG msg;
   
    while ( ::PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE ) )
    {  
        if ( ::GetMessage(&msg, NULL, 0, 0))
        {
            ::TranslateMessage(&msg);
            ::DispatchMessage(&msg);
        }
        else
        {
            break;
        }
    }
}

To stop long process set some flag:

void CMyDialog::OnStop()
{
    m_bStop = TRUE;

    // you can call EndDialog here
}

Long process nust be written by the following way:

void CMyDialog::LongProcess()
{
    m_bStop = FALSE;

    while ( ... )      // some loop
    {
          // do something ...

          DoEvents();          // handle events, here m_bStop may be set to TRUE

          if ( m_bStop )
              break;
     }
}
0
 

Author Comment

by:mathu76
ID: 13437143
Hi AlexFM

thanx for ur comments
I want to terminate the dialog box before the long process actually starts.

I have tried to do it something like this:

CMyDialog::OnOK()
{
   ........
   ........
   int iRet = 1;
   EndDialog(iRet);

   //calling for the long process...
   ptr->StartLongProcess();
}

The StartLongProcess() belongs to some other class.

so i need to terminate the dialog before control enters the StartLongProcess() as the dialog is no more reqd.

How do i do this?

waiting for suggestions

Regards


   
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 13437227
Can you move this to the caller code?

CMyDialog dlg;
if ( dlg.DoModal() == IDOK )
    StartLongProcess();
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 61

Expert Comment

by:Julian Hansen
ID: 13437344
Either that or you need to launch our LongProcess in a separate thread.
0
 
LVL 1

Expert Comment

by:tom_schultz
ID: 13438856
In your main thread. Before you make the dialog appear. In you main program make a new Thread for prosessing the results from the dialog in question. Also make an Event.(CreateEvent with intiaial state set to FALSE).

In you thread:
     use the function WaitForSingleObject(...) if you only have one event. (you should also have a StopEvent, to end Thread if program exits or             user clicks cancel)
     if two or more events use WaitForMultipleObjects
example code for the thread.: (wont compile :) )
HANDLE hevents[2] = {pmainprogram->m_stopEvent,pmainprogram->m_DoProssesEvent};
while(true)
{
   nresult = WaitForMultipleObjects(2,hevents,FALSE,INFINITE);
   if (nresult == WAIT_OBJECT_0 +1)//prosess_event
        do the prosessing here......
   else
   {
    // nresult was either WAIT_OBJECT_0 == stop event. or a time out or some other error.
    break; //break either way.
   }
   }
}
Now in you Dialog....
on the OK button before doing the Dialog::OnOK(); insert the line. SetEvent(pmainprogram->m_DoProssesEvent);
... this will start your thread with thre prosessing....

I hope this helps....
any questions or futher assistanse please contact me....
0
 

Author Comment

by:mathu76
ID: 13439198
hi AlexFM
I have tried what u have suggested but results are same.
Still the dialog box is not terminated.

Waiting for suggestions

Regards

0
 
LVL 48

Expert Comment

by:AlexFM
ID: 13439282
Please show your code. Dialog box is terminated after call to DoModal.
0
 

Author Comment

by:mathu76
ID: 13448102
My due apologies to AlexFM.
With the help of ur code the problem is solved.

Best thanx for suggestions

Best Regards
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
Suggested Courses

607 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