Solved

How to stop in the middle running algorithm??

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ADO Memory leak with DELPHI 2007 37 171
Strange date stored 22 106
Smart Camera scanning and reading information 3 100
mapAB Challlenge 35 128
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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…

863 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

27 Experts available now in Live!

Get 1:1 Help Now