SLEEP statement

Purpose | Pause the current thread of the application for a specified number of milliseconds (mSec), allowing other processes (or threads) to continue.
Syntax | SLEEP m&

Remarks

m& is the number of milliseconds (1 millisecond = 1/1000th of a second) to pause the application. Only the current thread pauses. If other threads are present, they will continue to execute. During the SLEEP period, all time-slices for the current thread are given to other threads and processes.

If m& is zero, the remainder of the current time-slice is relinquished. If there are no other threads of equal priority, execution continues immediately.

The time-slice duration (also known as the Quantum) can vary from version to version of Windows, ranging from 20 mSec to 120 mSec. Therefore, the Quantum can affect the performance of applications when SLEEP 0 is overused. That is, excessive use of SLEEP 0 can cause an application to cede much of its available processor time, causing a significant drop in application performance.

When code is running in a tight loop, it is quite possible to use up 100% of the available CPU time, so the occasional use of SLEEP 0 within a tight loop is often beneficial to overall performance of the target PC. For example, it may not be necessary to use SLEEP 0 for every iteration of a loop, but every second or third instead.

See also | THREAD CREATE, TIMER

Example

' Pause for 5 seconds

SLEEP 5000

' Release time-slice every 256 iterations

FOR x& = 0 TO &H0FFFFFFFF&

' code goes here

IF x& MOD 256 = 0 THEN SLEEP 0

NEXT x&