Functions and Directives

Function Sleep() Foundation

Explicitely sets idle condition for a thread.

Sleep( <nTime> ) --> NIL
<nTime> is a positive integer which specifies the time interval in 1/100 seconds during which a thread is to remain in an idle condition.

The return value of Sleep() is always NIL.


The function Sleep() defines an explicit idle condition for the thread in which the function is called. During the idle condition the thread uses no system sources (CPU time) and leaves resources available to other threads. The idle condition lasts <nTime> hundredths of a second and cannot be interrupted.

Idle conditions can also be defined with the function InKey() or AppEvent(). But with these functions a program waits until either an event takes place or until the defined time interval has passed. Neither function guarantees that the idle time interval will be completed. In addition, events can be lost when an idle condition is defined with Inkey() or AppEvent(). With Sleep() the time interval for the idle condition is guaranteed. Events which occur during the idle condition are stored in the event queue and are available after the completion of Sleep().

Defines idle condition using Sleep()

// The example attempts to lock a data record. 
// If a lock cannot be set, the program 
// waits 1/2 second and attempts to lock the record again. 
// After five attempts, termination occurs with a warning 
// notice. 

   LOCAL nMaxRetry := 5, nTries := 0, lUpdated := .F. 

   USE Customer 

   DO WHILE (nTries < nMaxRetry) .AND. !lUpdated 
      IF ! RLock()                         // attempt to lock record 
         Sleep( 50 )                       // wait 1/2 second 
         nTries ++                         // if lock fails 
         REPLACE Customer->Name WITH "Jones" // update data record 
         lUpdated := .T. 
                                           // data record is locked 
   IF ! lUpdated                           // more than 2.5 seconds 
      Alert( "Data record is currently locked", {"Ok"} ) 


If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.