Examples
The following examples illustrate the use of timed waiting and conditional waiting. In some cases, differences between the older classes and their new counterparts are highlighted.
Delaying
The following examples pause the current process for a certain amount of time or until a particular time.
Example: If you use the Delay class to wait for a set number of seconds or milliseconds, e.g.
[(Delay forSeconds: 15) wait.
Transcript show: 'Done waiting'; cr] fork.
you could use EsTimedWait instead, e.g.
[EsTimedWait forSeconds: 15.
Transcript show: 'Done waiting'; cr] fork.
In either example the Transcript Output: shows 'Done waiting after 15 seconds.
Example: If you use the Delay class to wait until a particular time,
e.g.
| time |
time := Time now.
Transcript cr; show: time asString.
 
Delay untilMilliseconds: (time asMilliseconds + 50000000).
 
Transcript cr; show: 'Done with delay', Time now asString.
You could use EsConditionalWait:
| time |
time := Time now.
Transcript cr; show: time asString.
 
(EsConditionalWait
until: [ (time asMilliseconds + 50000000) >=
(Time now asMilliseconds)]
ifTimeoutDo: [Transcript cr; show: 'timed out']) waitAndReturn.
 
Transcript cr; show: 'Done with delay at ', Time now asString.
 
Timed waiting
The AbtTimedWait or EsTimedWait class can wait for a set number of seconds or milliseconds.
In either of the examples below the Transcript Output: shows 'Done waiting after 15 seconds.
e.g.
AbtTimedWait forSeconds: 15.
Transcript show: 'Done waiting'; cr.
Transcript cr; show: 'Done with waiting at ', Time now asString.
e.g.
EsTimedWait forSeconds: 15.
Transcript show: 'Done waiting'; cr.
Transcript cr; show: 'Done with waiting at ', Time now asString.
Conditional waiting
AbtConditionalWait and EsConditionalWait classes both can wait for a particular condition.
The conditional wait mechanism is to repeatedly execute the condition block until the block returns true or a timeout is reached. If the timeout happens, the timeout block is executed. The Simple Example shows the mechanism. The File Example shows a more complicated example.
Simple Examples:
This AbtConditionalWait example returns immediately.
AbtConditionalWait
forCondition: [ true ]
ifTimeoutDo: [^CwMessagePrompter confirm: 'waited too long'].
The following example times out.
AbtConditionalWait
forCondition: [ false ]
ifTimeoutDo: [^CwMessagePrompter confirm: 'waited too long'].
This EsConditionalWait equivalent returns immediately.
EsConditionalWait
forCondition: [ true ]
ifTimeoutDo: [^CwMessagePrompter confirm: 'waited too long'].
The following EsConditionalWait equivalent times out.
EsConditionalWait
forCondition: [ false ]
ifTimeoutDo: [^CwMessagePrompter confirm: 'waited too long'].
 
File Example:
This example keeps trying to open a file until it exists or times out. You can change the file name to explore what happens when the file does or does not exist on your file system. You can create the file while the condidtion is being tested to change explore what happens in the first workspace.
 
The first workspace repeatedly tries to open a possibly nonexistent file.
[| boolean file |
EsConditionalWait
forCondition: [
(file := FileStream open: 'myfile.txt' oflag: ORDONLY) isCfsError not. ]
ifTimeoutDo: [
^CwMessagePrompter
warningMessage: 'file does not exist' ].
file atEnd
ifTrue: [
CwMessagePrompter
confirm: 'empty file']
ifFalse: [
CwMessagePrompter
message: 'nonempty '
title: 'file' ].
file close.] fork.
 
The second workspace is one way to create the file.
"while performing the above, create a file named 'myfile.txt'"
| file |
(file := FileStream open: 'myfile.txt' oflag: ORDONLY) isCfsError not.
 
file isCfsError
ifTrue: [file := FileStream open: 'myfile.txt' oflag: ORDONLY | OCREAT].
file close.
 
Last modified date: 09/18/2020