?
Solved

Problem with immediate termination of a dialog box?

Posted on 2005-03-01
8
Medium Priority
?
280 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
[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
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
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
LVL 59

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

719 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