DELAY creates a timed delay, pause or wait. DELAY will provide a time resolution of 1 second.
Syntax:DELAY NumSeconds AS INTEGER Parameters:
|
DELAY 3 ' delay 3 seconds
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:
|
The SLEEP procedure is a simple wrapper for the Win32 API Sleep statement. For more information, visit the Microsoft Sleep function webpage
SLEEP(1234) ' delay 1,234 milliseconds
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
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