11 System Clock

Programmed Delays

The WAIT Statement

The WAIT statement delays program execution a specified number of milliseconds before continuing. Syntax for this statement is as follows:

WAIT [numeric expression]

The numeric expression can range from -2147483648 through 2147483647 (about 596 hours); a negative number defaults to 0. The wait can be interrupted by pressing BREAK or a user-defined special function key (softkey or SFK).

Consider the following program extract:

2010 WAIT 8000
The WAIT command with a time interval (in milliseconds) provides the specified delay. However, this approach has the following potential difficulties:

  1. The resolution of the timed WAIT command is one millisecond.

  2. The delay of a timed WAIT command is terminated when any software interrupt condition becomes true and the specified action command occurs. Software interrupt conditions include softkeys, BREAK key, etc.

  3. Although software interrupts may be inhibited by means of the DISABLE command, this programming approach is not the friendliest to the end-user. Disabling software interrupts means that the Eloquence program is unable to detect conditions, such as input available, during the delay.

NOTE: Although the CLOCK function can be used to establish an arbitrary programmed delay, total computer system performance may suffer. Consider the following program extract:

2010 Start=CLOCK
2020 WHILE CLOCK-Start%<8000
2030 END WHILE
NOTE: This code results in an eight second programmed delay; however, the delay is a busy wait, meaning that the program demands processor execution time for the entire period of the delay. On a multiple user configuration, another process would effectively execute at half speed during the eight seconds.

The SLEEP Statement

The SLEEP statement behaves like WAIT unless executing in BACKGROUND. BACKGROUND is defined as redirecting stdout or setting -b flag on commandline. If executing in BACKGROUND the WAIT statement behaves differently:

This behaviour is acceptable for most programs. But if a program is dealing with external devices (e.g. modems or BDE) this may make it impossible to execute such programs in BACKGROUND.

Sample code:

   ON KEY#8:"EXIT" GOTO E
   REQUEST #11
   PRINTER IS 11
   ON INPUT #11 GOSUB P
   WAIT
E: STOP

P: A$=AREAD$(11)
   ...
   PRINT ">"
   WAIT 500
   PRINT "<"
   RETURN
This program works well and may be used to get data from PORT #11 until KEY#8 is pressed. Now we are in BACKGROUND - what happens is as follows: The WAIT statement will read the next line from stdin. If there is no more line on stdin the program will be terminated at once. The only valid input is ":KEY #8" which will terminate the program. The timed DELAY in the subprogram will be ignored. The program will not work.

The solution for the above problem is the SLEEP statement. If you replace the WAIT statement with a SLEEP statement, this program will behave in background the same as in foreground.

NOTE: Don't exchange WAIT with SLEEP statement without further investigation: SLEEP will not read stdin, so it's impossible to trigger a key "press" via stdin.


Eloquence Language Manual - 19 DEC 2002