Solved

Urgent!! How to cancel an operation which is running currently?

Posted on 2000-03-20
11
166 Views
Last Modified: 2013-11-20
I have a dialog that do stereo fusion processing. It takes about 4 minutes to accomplish the fusion task. What if the user wants to stop/cancel the process while it is running, so that the process is terminated? I've created a stop button. How can I do this? Can anyone pls provide me with the appropriate source code? Thank you very much!!
0
Comment
Question by:wanchin
  • 4
  • 3
  • 2
  • +2
11 Comments
 

Expert Comment

by:kply
Comment Utility
You can put one button on your dialog with the use of resource editor and using class wizard and map the message
BN_CLICKED for that button.



On that function give the following code

void YourDialog::OnExitButtonClicked()
{
      //only you write the following
        //single line of code

     PostMessage(WM_QUIT,0,0);
}

0
 
LVL 8

Expert Comment

by:VinExpert
Comment Utility
Hi,

The info is less. bcoz how the processing is done?, a continous loop?.
But I can suggest one method. Have a boolean flag. Continue processing if that flag is true. And break the processing if that flag is false. Somewhere in the processing time u have to check that flag regularly. And whenever user presses the stop button, make that flag as false.

There is one more method. Have the flag in the same way. Do Ur processing in a thread. In the thread check for that flag.

Try it out.
VinExpert
0
 
LVL 30

Expert Comment

by:Zoppo
Comment Utility
Hi wanchin,

To VinExpert: if it's really a continuous loop just a flag want solve the problem, because the dialog doesn't recieve the buttons press message until the loop terminates. I.e:

CMyDlg::Calc()
{
 while( ... ) // or for
 {
  // do lengthy calculation here
 }
}

The problem with such a loop is that no messages can be processed while loop is running. For this you have several ways:

- redesign your calculation to be done in idle time, when dialog does not recieve any messages. This could be a lot of work.
- move your calculation to a seperate thread. Even a lot of work with synchronization.
- Within you loop check incoming messages and dispatch them, i.e. like this

CMyDlg::Calc()
{
 int count = 0;
 MSG msg;
 m_bContinueLoop = TRUE;
 while( m_bContinueLoop ) // m_bContinueLoop is the flag which is reset in the stop buttons message handler
 {
  if ( ( ++count % 100 ) == 0 )
  {
   // every hundreth loop check messages
   while ( ::PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
   {
    ::DispatchMessage( &msg );
    if ( msg.message == WM_CLOSE ) // you should check for WM_QUIT and WM_DESTROY also...
     return; // break calculation with WM_CLOSE
   }
  }
  // do lengthy calculation here
 }
}

this will allow your dialog to process message as usual.

Now you can simply call i.e. m_bContinueLoop = FALSE; from the stop button's message handler.

hope that helps,

ZOPPO
0
 
LVL 8

Expert Comment

by:VinExpert
Comment Utility
Hi,

Yes, But a thread will be a better solution I feel.

VinExpert
0
 
LVL 30

Expert Comment

by:Zoppo
Comment Utility
Well, I think idle time processing would be even good, but both may need lots of work for redesign.

ZOPPO
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:wanchin
Comment Utility
Zoppo,
I've tried your code but it doesn't work. perhaps i've made some mistakes while inserting the code.
I attach the code of my continuos loop process fuction and a stopbutton function. can you show me exactly where should i make alteration of the code?

void CStereoFusionDlg::OnFusionButton()
{
      int X, Y;
      int i,j, xL, xR, y;
      int dispX, dispY;
      int diff, total_diff, min_diff, posXLeft, posYLeft;
      int posXLeft2, posYLeft2;
      count = 0;

      m_stop.EnableWindow(TRUE);

      for (y=10; y<=245; y++)
      {      
          for (xR=10; xR<=245; xR++)
            {
                  min_diff = 30000;

                  for (xL=10; xL<=245; xL++)
                  {
                        total_diff = 0;
                        for (i=-10; i<=10; i++)
                        {
                              for (j=-10; j<=10; j++)
                              {
                                    diff = abs(RightBuf[y+i][xR+j] - LeftBuf[y+i][xL+j]);
                                    total_diff += diff;
                              }
                        }
                        
                        if(total_diff < min_diff)
                        {
                              min_diff = total_diff;
                              posXLeft = xL;
                              posYLeft = y;
                        }
                        
                  }

                  for (i=-10; i<=10; i++)
                        for (j=-10; j<=10; j++)
                        {      
                              diff = abs(RightBuf[y][xR] - LeftBuf[posYLeft+i][posXLeft+j]);
                                                      
                              if(diff < min_diff)
                              {
                                    min_diff = diff;
                                    posXLeft2 = posXLeft + j;
                                    posYLeft2 = posYLeft + i;
                              }
                        }      

                  X = xR;
                  Y = y;
                  dispX = xR - posXLeft;
                  dispX *= dispX;
                  dispY = y - posYLeft;
                  dispY *= dispY;
                  disparity_buf[Y][X] = sqrt(dispX + dispY);
                  cyclopean_buf[Y][X] = (( RightBuf[y][xR] + LeftBuf[posYLeft2][posXLeft2] ) / 2);
                  
            }

            m_progress1.SetPos(count);
            count++;
      }
      
      DisplayDisparityMap();
      DisplayCyclopeanView();
}

void CStereoFusionDlg::OnStopButton()
{
      // TODO: Add your control notification handler code here
}
0
 

Author Comment

by:wanchin
Comment Utility
Adjusted points from 100 to 150
0
 

Expert Comment

by:jfhuang
Comment Utility
I have designed a CWaitDialog in one of my project. WaitDialog has a label control and progress bar control and a cancel command button. use can click the cancle button to cancel the processing.  

CWaitDialog has methods:

1. SetMessageText(char *szText) for you to display message  at every stage of processing.
2. SetPercentComplete(int nIndex) for you to set the progress bar

the sample of the calling code could be  as follows:
bool bContinue = true;
CWaitDialog myDlg(&bContinue, "stereo fusion processing");     // give the dialog title and user cancel status variable bContinue.
myDlg.SetMessageText("Begin Processing ...");
nInc = 0;nIndicator= 0;
for (long i = 0;i< 3000; i++){
    if(!bContinue) break; // user cancellation

   if (nInc++ == 30){
     nInc = 0;                   
     myDlg.SetPercentComplete(nIndicator++);
   }
}

if you are interested to use this waitdialog, I can give you the source code of this class.
0
 
LVL 30

Expert Comment

by:Zoppo
Comment Utility
Hi wanchin,

I would put it inside outside the first 'i'-loop (but you can even try if it's enough to put it outside the 'xR'.loop, depends on average reaction time), like this:

// to class add a flag in .h file
class CStereoFusionDlg : ...
{
 ...
 BOOL m_bCancel;
 ...
}


// in function
MSG msg;
m_bCancel = FALSE;

for (y=10; y<=245; y++)
{
 for (xR=10; xR<=245; xR++)
 {
  min_diff = 30000;

  for (xL=10; xL<=245; xL++)
  {
   total_diff = 0;
   for (i=-10; i<=10; i++)
   {
    for (j=-10; j<=10; j++)
    {
     diff = abs(RightBuf[y+i][xR+j] - LeftBuf[y+i][xL+j]);
     total_diff += diff;
    }
   }

   while ( ::PeekMessage( &msg, m_hWnd, 0, 0, PM_REMOVE ) )
   {
     ::DispatchMessage( &msg );
     if ( msg.message == WM_CLOSE ) // check for WM_QUIT, WM_DESTROY
      return; // break calculation with WM_CLOSE
   }
   if ( m_bCancel )
    return;                        
....
  }
 }
}

void CStereoFusionDlg::OnStopButton()
{
 m_bCancel = TRUE;
}

hope that helps,

ZOPPO
0
 

Author Comment

by:wanchin
Comment Utility
Thank you very much, Zoppo! It works now. How can i transfer the points to you?

wanchin
0
 
LVL 30

Accepted Solution

by:
Zoppo earned 150 total points
Comment Utility
I just answer this :)

BTW, there should be an option 'accept comment as answer'...

ZOPPO
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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 …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

728 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

8 Experts available now in Live!

Get 1:1 Help Now