Solved

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

Posted on 2000-03-20
11
167 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
ID: 2635352
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
ID: 2635358
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 31

Expert Comment

by:Zoppo
ID: 2635430
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 8

Expert Comment

by:VinExpert
ID: 2635527
Hi,

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

VinExpert
0
 
LVL 31

Expert Comment

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

ZOPPO
0
 

Author Comment

by:wanchin
ID: 2638961
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
ID: 2638965
Adjusted points from 100 to 150
0
 

Expert Comment

by:jfhuang
ID: 2639060
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 31

Expert Comment

by:Zoppo
ID: 2639273
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
ID: 2642396
Thank you very much, Zoppo! It works now. How can i transfer the points to you?

wanchin
0
 
LVL 31

Accepted Solution

by:
Zoppo earned 150 total points
ID: 2643680
I just answer this :)

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

ZOPPO
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
wait notify demo infinite loop 3 110
Path of Workbook 3 77
Making an alias 7 95
Increment column based of a FK 8 21
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

773 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