Try something like this: | process | process := [ x := MyClass2 new. x myLoop ] fork. (Delay forSeconds: 10) wait. process terminate. With #fork, you are running the loop in a background process. #fork returns the process instance. Wait a bit using Delay and then send #terminate to the process ... If you stash `process` into a global you can let it run away while you do work in the foreground. Depending upon how tight the loop is, you may need to put in a `Processor yield` statement to give the foreground process a chance to run ... you should also look into process priorities. Dale On 04/21/2011 01:08 PM, DougEdmunds wrote:
I created a class that generates an endless loop. For this example, the loop just prints 1-10, back and forth. I'd like to stop the loop by using code, but when it is running, I can't type anything in a workspace. The only way I can stop it is by a manual interrupt (on XP, using Alt-<period key>)
Typical run: x := MyClass2 new. x myLoop.
I want to type "x stopLoop" which would set the running variable to false.
How do I do this?
--------
'From Pharo1.3a of ''18 January 2011'' [Latest update: #13144] on 21 April 2011 at 12:59:14 pm'! Object subclass: #MyClass2 instanceVariableNames: 'running' classVariableNames: '' poolDictionaries: '' category: 'Sandbox0419'!
!MyClass2 methodsFor: 'as yet unclassified' stamp: 'DougEdmunds 4/21/2011 12:54'! initialize super initialize. running := true. Transcript clear. Transcript show: 'try to stop the loop somehow' . ! !
!MyClass2 methodsFor: 'as yet unclassified' stamp: 'DougEdmunds 4/21/2011 12:54'! myLoop
"running is an instance variable, initialized to true."
| x up | Transcript clear. x := 1. up := true. [Transcript show: x; cr. x>= 10 ifTrue: [up :=false]. x<= 1 ifTrue: [up := true]. up ifTrue: [x := x + 1] ifFalse: [x := x - 1]. (Delay forSeconds: 1) wait. running ifFalse: [^ x]. ] repeat. ! !
!MyClass2 methodsFor: 'as yet unclassified' stamp: 'DougEdmunds 4/21/2011 12:55'! stopLoop running := false. ! ! --------
-- View this message in context: http://forum.world.st/How-to-break-out-of-endless-loop-using-code-tp3466747p... Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.