Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to stop in the middle running algorithm??

Posted on 2002-05-18
4
Medium Priority
?
222 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 200 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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…
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
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.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…

927 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