Solved

How to stop in the middle running algorithm??

Posted on 2002-05-18
4
217 Views
Last Modified: 2013-11-20
Hello,
I'm working on some MFC application which launches some quite complicated algorithm, which has many inside iterations and loops.
So , when user presses "Run Algorithm" button entire applications like "stuckes" until the algorithm finishes it's job. So, I would like to let user stop this algorithm in the middle by pressing some button. How am I implementing it? Maybe multithreads?
thanks
0
Comment
Question by:borlip
4 Comments
 
LVL 49

Accepted Solution

by:
DanRollins earned 50 total points
ID: 7018531
There are two main methods: Message pumping and multiple threads.  In both cases you need to add code like this to your 'complicated algorithm' code:

while( doing some long operation ) {
    // do some work
    if ( m_fAbort ) {
        return; // or just break out of loop
    }
}

=-=-=-=--==-=--==-=--==--==-=-=-=-
Method #1:  Message Pumping

Create this function

void PumpWaitingMessages()
{
        MSG msg;
        while ( ::PeekMessage( &msg, 0,0,0, PM_NOREMOVE) ) {
            AfxGetApp()->PumpMessage();
        }
}

And sprinkle call sot PumpWaitingMessages() through often-executed parts of your algorithm.

m_fAbort= FALSE;
while( doing some long operation ) {
    // do some work
    PumpWaitingMessages();
    // do some more work
    PumpWaitingMessages();
    if ( m_fAbort ) {
        return; // or just break out of loop
    }
}

Now you will need to have a button labled
     "Stop Long Operation"
and set up an OnClick handler for that button.  When the user clicks it, your handler will do simply

      m_fAbort= TRUE;

So that the algorithm will stop.

=-=-=-=--==-=--==-=--==--==-=-=-=-
Method #2:  Multiple threads
In this technique, you create a 'worker thread' that will actually perform the algorithm code.  You do not need to 'pump messages' because the main (user interface) thread will do that automatically.  Your algorithm code will simply watch for m_fAbort to get set to TRUE and exit when it is (as with Method #1).

Multi-threading adds many complications to your code and I really don't want to describe all of these problems or how to avoid them for 50 points.  So I recommend that you use Method #1 for now.

-- Dan
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Question has a verified solution.

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

Suggested Solutions

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.

828 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