DELAY statement

Purpose:

DELAY creates a timed delay, pause or wait. DELAY will provide a time resolution of 1 second.

Syntax:

DELAY NumSeconds AS INTEGER

Parameters:

  • Data type: INTEGER
    NumSeconds Number of seconds to delay.

Example:

DELAY 3 ' delay 3 seconds

SLEEP statement

Purpose:

SLEEP creates a timed pause with the minimum interval equivalent to a time resolution dependent on the speed of the system clock, typically, 15.625 millisecond, 64 ticks per second.

Syntax:

SLEEP[(Milliseconds AS INTEGER)]

Parameters:

  • Data type: INTEGER
    Milliseconds [OPTIONAL] Number of milliseconds to pause. SLEEP without an argument will wait for a key press. Parentheses are optional if an argument is used.
    👉 Please note that the system clock "ticks" at a constant rate. If Milliseconds is less than the resolution of the system clock, the thread may sleep for less than the specified length of time. If Milliseconds is greater than one tick but less than two, the wait can be anywhere between one and two ticks, and so on. To increase the accuracy of the sleep interval, please see Example 2: and Example 3: below.

Remarks:

The SLEEP procedure is a simple wrapper for the Win32 API Sleep statement. For more information, visit the Microsoft Sleep function webpage

Example 1:

SLEEP(1234) ' delay 1,234 milliseconds

Example 2:

Here is a version that is accurate to about 1 millisecond. However, it can sometimes affect other system timers resulting in stutters or flickers.

SUB BCX_SLEEP (x AS INTEGER)
  '****************************************** 
  ' Gives precision to about 1 millisecond 
  '****************************************** 
  DIM RAW tc AS TIMECAPS
  timeGetDevCaps(&tc, SIZEOF(TIMECAPS))
  timeBeginPeriod(tc.wPeriodMin)
  SLEEP(x)
  timeEndPeriod(tc.wPeriodMin)
END SUB

Example 3:

This version is good to about 100 nano-seconds but is setup to accept only milliseconds argument. It has lower impact on the system and could be extended to accept sub-millisecond arguments.

SUB BCX_SLEEP (Milliseconds AS INTEGER)
  '*************************************************** 
  ' Intervals are precise to approx 100 nano-seconds 
  ' BCX_SLEEP(0) resets this timer 
  ' Only use this when you need short, high precision 
  ' intervals (1 to  15 milliseconds) otherwise just 
  ' use BCX's normal SLEEP() command 
  '*************************************************** 
  STATIC hTimer AS HANDLE
  DIM liDueTime AS LARGE_INTEGER
  DIM success AS INT

  ' If called with 0, close and reset the timer handle 
  IF Milliseconds = 0 THEN
    IF hTimer THEN
      CloseHandle(hTimer)
      hTimer = NULL
    END IF
    EXIT SUB
  END IF

  ' Initialize timer if not already created 
  IF hTimer = NULL THEN
    hTimer = CreateWaitableTimer(NULL, TRUE, NULL)
    IF hTimer = NULL THEN EXIT SUB
  END IF

  ' Convert ms to 100ns intervals (negative = relative time) 
  liDueTime.QuadPart = -1 * Milliseconds * 10000

  success = SetWaitableTimer(hTimer, &liDueTime, 0, NULL, NULL, FALSE)
  IF success THEN
    WaitForSingleObject(hTimer, INFINITE)
  END IF
END SUB

PAUSE statement

Purpose:

PAUSE stops the running program and waits for a key to be pressed before allowing the program to continue. PAUSE is intended for use in CONSOLE mode programming.

Syntax:

PAUSE

Parameters:

  • None Output is "Press any key to continue . . ."