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-tp3466747p3466747.html
Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.