Jump to content
xisto Community
kvarnerexpress

Stop A Program Excecution

Recommended Posts

I made and MFC program with MSVC++ 6.0. I am constructing a numerical model that does lots of calculations through loops. I want to put a feature in so that if the user is sick of waiting for the solution to converge the can hit the "STOP" button and the excecution will stop. Is there a way I can do this? I put in a "Stop!" button and made a handler function OnStop(). What can I do from here?Thank you!

Share this post


Link to post
Share on other sites

Hmm, I don't know any C++, but I do know Visual Basic and Java, and there are statements for both languages that can stop the execution in the current block it's in. For example, in Visual Basic, if you want to get out of a For...Next loop, you'd use the

Exit For
statement like so:

Dim blah as IntegerFor blah = 1 To 99  blah = blah + 1  If blah = 45 Then    Exit For  End IfNext blah

You can also use the Exit statement in Visual Basic to break out of a sub or a function:
Exit Sub
or
Exit Function

In Java, you have the break statement which ends the current loop. You'd use it the same way as the Exit statement in VB.

I don't know if all that helped you, but you can try to find a statement in C++ that ends the execution in the block of code like VB or Java. :)

Share this post


Link to post
Share on other sites

snlildude87 has a good idea there. in c++ this particular statement is called the 'break;' statement, it is used for pulling out of loops and switch statements (probably other statements too).However, your MFC program will, once it is in a loop like this, have a hard time accepting user input, so you will find that upon clicking on your magic stop button, nothing will happen, in fact, the program will appear to stop responding. The way to work around this is to use a statement similar to Visual Basic's DoEvents() function. pretty much this checks for any messages that the window has to process (for example, WM_SIZE, WM_PAINT) and executes them. I think in VC++ these are TranslateMessage() and DispatchMessage(), however, i am not 100% sure on their usage.Hope i've helped!cheers, SwITCh?!

Share this post


Link to post
Share on other sites

I think you should spin off a little thread for the looping processes, so you can shut it down when you want it to.

 

This is probably going to sound vague but hopefully sets you off in the right direction, or at least spark some ideas.

 

e.g. - Oh, and this isn't using CWinThread::CreateThread, but ::CreateThread.

 

HANDLE loopThreadHandle = ::CreateThread(NULL, 0, loopFunction,

(void*)param, 0, NULL);

[\CODE]

 

Now, when onStop() is called, you can have somewhere inside...

 

[code=auto:0]

CloseHandle(loopThreadHandle);

[\CODE]

 

I can't be sure how'd you'd pass all your values around, but I'm sure if you bundled it up in a class properly, it could work.

 

The other option is creating a derived class for CWinThread that has all the looping in it... I could be wrong though, only having briefly touched on MFC.

Share this post


Link to post
Share on other sites

I'm sure there are other forum-retarded people too. Give us an edit button! :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.