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. ! ! -------- |
using different process and having synchronisation point to communicate with it?
Stef On Apr 21, 2011, at 10: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-tp3466747p3466747.html > Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com. > |
In reply to this post by DougEdmunds
Hi Doug,
If you run your code in a workspace with 'do-it' command, it runs in the UI smalltalk process and the only way to cancel it is the cancel key You shoud run your code in a separate process like: x := MyClass2 new [x myLoop ] forkAt: Processor systemBackgroundPriority. forkAt:... forks the block in another smalltalk process. like this you will be able to tell your x object to stop looping from your workspace (by setting a boolean in your object's member), or you could terminate it or debug it with the Process browser. HTH, Cheers Alain "DougEdmunds" <[hidden email]> a écrit dans le message de news: [hidden email]... >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. > > |
In reply to this post by DougEdmunds
On Thu, Apr 21, 2011 at 10:08 PM, DougEdmunds <[hidden email]> wrote: I created a class that generates an endless loop. For this example, the loop I have no idea how to do it. But all I know is that no matter whether you use a UI or type something manually, at the end everything is an object + message send. So..what I am saying is that you can check what the interruption keys does and do it by code or take the idea and do something similar. Check UserInterruptHandler, its users, etc... Cheers Mariano BTW if you find it, let us know Typical run: -- Mariano http://marianopeck.wordpress.com |
In reply to this post by DougEdmunds
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-tp3466747p3466747.html > Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com. > |
Free forum by Nabble | Edit this page |