Solved

How to stop in the middle running algorithm??

Posted on 2002-05-18
4
207 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

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: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

747 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

12 Experts available now in Live!

Get 1:1 Help Now