Solved

How to stop in the middle running algorithm??

Posted on 2002-05-18
4
220 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

628 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