Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Stop A Program Excecution
kvarnerexpress
post Mar 26 2005, 09:21 PM
Post #1


Super Member
*********

Group: Members
Posts: 407
Joined: 13-December 04
Member No.: 2,696



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!
Go to the top of the page
 
+Quote Post
snlildude87
post Mar 27 2005, 02:27 AM
Post #2


Moderator
***************

Group: Members
Posts: 2,325
Joined: 8-March 05
From: Mawson, Antarctica
Member No.: 4,254



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
CODE
Exit For
statement like so:

CODE

Dim blah as Integer

For blah = 1 To 99
 blah = blah + 1

 If blah = 45 Then
   Exit For
 End If
Next blah


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

Exit Sub

or
CODE

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. smile.gif
Go to the top of the page
 
+Quote Post
switch
post Mar 29 2005, 11:31 PM
Post #3


Premium Member
********

Group: Members
Posts: 178
Joined: 13-October 04
From: NSW, Australia
Member No.: 1,713



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?!
Go to the top of the page
 
+Quote Post
dexter
post Mar 30 2005, 04:16 AM
Post #4


Advanced Member
*******

Group: Members
Posts: 142
Joined: 24-December 04
From: Queensland, Australia
Member No.: 2,902



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.

[CODE]
HANDLE loopThreadHandle = ::CreateThread(NULL, 0, loopFunction,
(void*)param, 0, NULL);
[\CODE]

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

[CODE]
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.
Go to the top of the page
 
+Quote Post
dexter
post Mar 30 2005, 04:21 AM
Post #5


Advanced Member
*******

Group: Members
Posts: 142
Joined: 24-December 04
From: Queensland, Australia
Member No.: 2,902



I'm sure there are other forum-retarded people too. Give us an edit button! tongue.gif
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Program Flow(2)
  2. Timeouting Program(2)
  3. Crazy Looking C Program(19)
  4. C++(15)
  5. Run A Program From Another One(4)
  6. Program From Mekka 97 4k(2)
  7. Need Debugging Help For A Simple Program(3)
  8. Need Help With C Program To Test If A Number Is Prime(12)
  9. Executing An Exe From Within A Program!(7)
  10. My First C++ Program(6)
  11. Source Code For Paint Like Program Under Dos In C(2)
  12. Program Lang I Know(2)
  13. Centering The Text In A Simple C++ Program?(8)
  14. Buffer Overflow In Action Tutorial(0)
  15. C Or C++ Easy Programming Generator(6)
  1. A C++ Program Print Hello With Nothing Written In Main() Method(2)
  2. A Crazy Program In C(13)
  3. How To Run A Process Through Your Program (c#)(0)
  4. Any Program Ideas?!(5)
  5. If/else Demo Program Bug(4)


 



- Lo-Fi Version Time is now: 12th October 2008 - 03:29 PM