Block.st
changeset 24367 b4d803255efe
parent 24366 7e7c5d3c4a81
child 24368 fa4fbc381d85
--- a/Block.st	Wed Jun 26 11:51:26 2019 +0200
+++ b/Block.st	Wed Jun 26 11:52:22 2019 +0200
@@ -2280,6 +2280,174 @@
     "
 ! !
 
+!Block methodsFor:'evaluation with timeout'!
+
+valueWithConfirmedTimeout:secondsOrTimeDuration confirmWith:confirmationBlock
+    "evaluate the receiver.
+     If not finished after secondsOrTimeDuration, call the confirmationBlock.
+     If it returns true, another time-interval is setup and we continue waiting.
+     If it returns a number (seconds) or a timeDuration, this time-interval is setup and we continue waiting.
+     If it returns false, nil is returned immediately."
+
+    |ok retVal interrupter|
+
+    ok := false.
+    interrupter := [ ok ifFalse:[ TimeoutError raiseRequest ] ].
+    [
+        Processor addTimedBlock:interrupter after:secondsOrTimeDuration.
+
+        TimeoutError handle:[:ex |
+            |answer nextWaitTime|
+            
+            answer := confirmationBlock valueWithOptionalArgument:ex.
+            answer == false ifTrue:[
+                ^ nil
+            ].
+            answer == true ifTrue:[
+                nextWaitTime := secondsOrTimeDuration
+            ] ifFalse:[
+                nextWaitTime := answer asTimeDuration
+            ].    
+            "/ proceed, setting up another timeout
+            Processor addTimedBlock:interrupter after:nextWaitTime.
+            ex proceed
+        ] do:[     
+            retVal := self value.
+            ok := true.
+        ].
+    ] ensure:[
+        Processor removeTimedBlock:interrupter.
+    ].
+    ^ retVal
+    
+    "
+     [
+        1 to:10 do:[:i | 
+            Transcript showCR:i.
+            1 seconds wait. 
+        ].
+        'finished'
+     ] valueWithConfirmedTimeout:(3 seconds) confirmWith:[
+        (Dialog confirm:'continue?')
+     ].
+    "
+    
+    "
+     [
+        1 to:10 do:[:i | 
+            Transcript showCR:i.
+            1 seconds wait. 
+        ].
+        'finished'
+     ] valueWithConfirmedTimeout:(3 seconds) confirmWith:[
+        (Dialog confirm:'wait another 5 seconds?') ifTrue:[
+            5
+        ] ifFalse:[
+            false
+        ].
+     ].
+    "
+
+    "Created: / 26-06-2019 / 11:46:02 / Claus Gittinger"
+!
+
+valueWithTimeout:aTimeDurationOrIntegerSeconds
+    "execute the receiver, but abort the evaluation after aTimeDuration if still running.
+     Return the receiver's value, or nil if aborted."
+
+    |milliseconds|
+
+    milliseconds := aTimeDurationOrIntegerSeconds isTimeDuration
+                        ifTrue:[ aTimeDurationOrIntegerSeconds asMilliseconds ]
+                        ifFalse:[ (aTimeDurationOrIntegerSeconds * 1000) truncated].
+    ^ self valueWithWatchDog:[^ nil] afterMilliseconds:milliseconds
+
+    "
+     [
+        1 to:15 do:[:round |
+            Transcript showCR:round.
+            Delay waitForMilliseconds:20.
+        ].
+        true
+     ] valueWithTimeout:(TimeDuration seconds:1)
+    "
+
+    "
+     [
+        1 to:100 do:[:round |
+            Transcript showCR:round.
+            Delay waitForMilliseconds:20.
+        ].
+        true
+     ] valueWithTimeout:(TimeDuration seconds:1)
+    "
+!
+
+valueWithWatchDog:exceptionBlock afterMilliseconds:aTimeLimit
+    "a watchdog on a block's execution. If the block does not finish its
+     evaluation after aTimeLimit milliseconds, it is interrupted (aborted) and
+     exceptionBlock's value is returned. The receiver's code must be prepared
+     for premature returning (by adding ensure blocks, as required)"
+
+    |inError|
+
+    inError := false.
+
+    ^ TimeoutNotification handle:[:ex |
+        inError ifTrue:[
+            ex proceed
+        ].
+        exceptionBlock valueWithOptionalArgument:ex.
+    ] do:[
+        NoHandlerError handle:[:ex |
+            inError := true.
+            ex reject.
+        ] do:[
+            |showStopper me done|
+
+            done := false.
+            me := Processor activeProcess.
+            showStopper := [
+                    done ifFalse:[
+                        me interruptWith:[
+                            (done not and:[me isDebugged not]) ifTrue:[ 
+                                TimeoutNotification raiseRequest.
+                            ]
+                        ]
+                    ]
+                ].
+
+            [
+                |retVal|
+
+                Processor
+                    addTimedBlock:showStopper
+                    for:me
+                    afterMilliseconds:aTimeLimit.
+
+                retVal := self value.
+                done := true.
+                retVal
+            ] ensure:[
+                Processor removeTimedBlock:showStopper
+            ].
+        ]
+    ].
+
+    "
+     [
+        Delay waitForSeconds:5.
+        true
+     ] valueWithWatchDog:[false] afterMilliseconds:2000
+    "
+
+    "Modified: / 21-05-2010 / 12:19:57 / sr"
+    "Modified: / 19-03-2017 / 18:13:07 / cg"
+    "Modified: / 31-01-2018 / 08:34:51 / stefan"
+    "Modified: / 23-05-2018 / 12:47:00 / Maren"
+    "Modified (comment): / 29-05-2019 / 01:50:41 / Claus Gittinger"
+! !
+
 !Block methodsFor:'exception handling'!
 
 on:aSignalOrSignalSetOrException do:exceptionBlock
@@ -2475,75 +2643,6 @@
     "Modified: / 26.7.1999 / 15:30:48 / stefan"
 !
 
-valueWithConfirmedTimeout:secondsOrTimeDuration confirmWith:confirmationBlock
-    "evaluate the receiver.
-     If not finished after secondsOrTimeDuration, call the confirmationBlock.
-     If it returns true, another time-interval is setup and we continue waiting.
-     If it returns a number (seconds) or a timeDuration, this time-interval is setup and we continue waiting.
-     If it returns false, nil is returned immediately."
-
-    |ok retVal interrupter|
-
-    ok := false.
-    interrupter := [ ok ifFalse:[ TimeoutError raiseRequest ] ].
-    [
-        Processor addTimedBlock:interrupter after:secondsOrTimeDuration.
-
-        TimeoutError handle:[:ex |
-            |answer nextWaitTime|
-            
-            answer := confirmationBlock valueWithOptionalArgument:ex.
-            answer == false ifTrue:[
-                ^ nil
-            ].
-            answer == true ifTrue:[
-                nextWaitTime := secondsOrTimeDuration
-            ] ifFalse:[
-                nextWaitTime := answer asTimeDuration
-            ].    
-            "/ proceed, setting up another timeout
-            Processor addTimedBlock:interrupter after:nextWaitTime.
-            ex proceed
-        ] do:[     
-            retVal := self value.
-            ok := true.
-        ].
-    ] ensure:[
-        Processor removeTimedBlock:interrupter.
-    ].
-    ^ retVal
-    
-    "
-     [
-        1 to:10 do:[:i | 
-            Transcript showCR:i.
-            1 seconds wait. 
-        ].
-        'finished'
-     ] valueWithConfirmedTimeout:(3 seconds) confirmWith:[
-        (Dialog confirm:'continue?')
-     ].
-    "
-    
-    "
-     [
-        1 to:10 do:[:i | 
-            Transcript showCR:i.
-            1 seconds wait. 
-        ].
-        'finished'
-     ] valueWithConfirmedTimeout:(3 seconds) confirmWith:[
-        (Dialog confirm:'wait another 5 seconds?') ifTrue:[
-            5
-        ] ifFalse:[
-            false
-        ].
-     ].
-    "
-
-    "Created: / 26-06-2019 / 11:46:02 / Claus Gittinger"
-!
-
 valueWithExceptionHandler:handler
     "evaluate myself. If any of the signals in handler is raised,
      evaluate the corresponding handler block."
@@ -2556,103 +2655,6 @@
 
     "Created: / 26.7.1999 / 11:23:45 / stefan"
     "Modified: / 26.7.1999 / 11:24:06 / stefan"
-!
-
-valueWithTimeout:aTimeDurationOrIntegerSeconds
-    "execute the receiver, but abort the evaluation after aTimeDuration if still running.
-     Return the receiver's value, or nil if aborted."
-
-    |milliseconds|
-
-    milliseconds := aTimeDurationOrIntegerSeconds isTimeDuration
-                        ifTrue:[ aTimeDurationOrIntegerSeconds asMilliseconds ]
-                        ifFalse:[ (aTimeDurationOrIntegerSeconds * 1000) truncated].
-    ^ self valueWithWatchDog:[^ nil] afterMilliseconds:milliseconds
-
-    "
-     [
-        1 to:15 do:[:round |
-            Transcript showCR:round.
-            Delay waitForMilliseconds:20.
-        ].
-        true
-     ] valueWithTimeout:(TimeDuration seconds:1)
-    "
-
-    "
-     [
-        1 to:100 do:[:round |
-            Transcript showCR:round.
-            Delay waitForMilliseconds:20.
-        ].
-        true
-     ] valueWithTimeout:(TimeDuration seconds:1)
-    "
-!
-
-valueWithWatchDog:exceptionBlock afterMilliseconds:aTimeLimit
-    "a watchdog on a block's execution. If the block does not finish its
-     evaluation after aTimeLimit milliseconds, it is interrupted (aborted) and
-     exceptionBlock's value is returned. The receiver's code must be prepared
-     for premature returning (by adding ensure blocks, as required)"
-
-    |inError|
-
-    inError := false.
-
-    ^ TimeoutNotification handle:[:ex |
-        inError ifTrue:[
-            ex proceed
-        ].
-        exceptionBlock valueWithOptionalArgument:ex.
-    ] do:[
-        NoHandlerError handle:[:ex |
-            inError := true.
-            ex reject.
-        ] do:[
-            |showStopper me done|
-
-            done := false.
-            me := Processor activeProcess.
-            showStopper := [
-                    done ifFalse:[
-                        me interruptWith:[
-                            (done not and:[me isDebugged not]) ifTrue:[ 
-                                TimeoutNotification raiseRequest.
-                            ]
-                        ]
-                    ]
-                ].
-
-            [
-                |retVal|
-
-                Processor
-                    addTimedBlock:showStopper
-                    for:me
-                    afterMilliseconds:aTimeLimit.
-
-                retVal := self value.
-                done := true.
-                retVal
-            ] ensure:[
-                Processor removeTimedBlock:showStopper
-            ].
-        ]
-    ].
-
-    "
-     [
-        Delay waitForSeconds:5.
-        true
-     ] valueWithWatchDog:[false] afterMilliseconds:2000
-    "
-
-    "Modified: / 21-05-2010 / 12:19:57 / sr"
-    "Modified: / 19-03-2017 / 18:13:07 / cg"
-    "Modified: / 31-01-2018 / 08:34:51 / stefan"
-    "Modified: / 23-05-2018 / 12:47:00 / Maren"
-    "Modified (comment): / 29-05-2019 / 01:50:41 / Claus Gittinger"
 ! !
 
 !Block methodsFor:'exception handling private'!