FreeBasic Sleep Keyword
last modified June 16, 2025
The FreeBasic Sleep
keyword pauses program execution for a
specified number of milliseconds. It's useful for creating delays,
controlling timing, and reducing CPU usage in loops.
Basic Definition
In FreeBasic, Sleep
is a built-in procedure that suspends
program execution. It takes one parameter - the number of milliseconds
to pause.
The Sleep
function is platform-independent and provides a
simple way to add delays. It's commonly used in games, animations, and
user interfaces to control timing.
Simple Sleep Example
This basic example demonstrates the simplest usage of the Sleep keyword.
Print "Program started" Sleep 2000 ' Pause for 2 seconds Print "Program resumed after 2 seconds"
Here we print a message, pause execution for 2000 milliseconds (2 seconds), then print another message. The Sleep call blocks all program execution during the pause.
Sleep in a Loop
Sleep is often used in loops to control the speed of iteration.
For i As Integer = 1 To 5 Print "Count: "; i Sleep 1000 ' Pause for 1 second between counts Next
This example counts from 1 to 5 with a 1-second delay between each number. Without Sleep, the loop would execute too quickly to see each count.
Creating a Simple Timer
We can use Sleep to build a basic countdown timer.
Dim seconds As Integer = 10 While seconds > 0 Print "Time remaining: "; seconds; " seconds" Sleep 1000 seconds -= 1 Wend Print "Time's up!"
This creates a 10-second countdown timer that updates every second. The Sleep ensures each iteration takes exactly 1 second, making the timer accurate.
Animation with Sleep
Sleep can help create simple text animations by controlling frame rate.
Dim frames(3) As String = {"-", "\", "|", "/"} For i As Integer = 1 To 20 Print "Loading "; frames(i Mod 4); " "; i * 5; "%" Sleep 150 ' 150ms delay creates smooth animation Cls ' Clear screen for next frame Next
This example shows a spinning loader animation with percentage progress. The Sleep controls animation speed. Without it, the animation would be too fast to see.
User Input Delay
Sleep can provide a pause before requesting user input.
Print "Important message incoming..." Sleep 3000 Print "The system will reboot in 5 minutes!" Print "Press any key to cancel..." Sleep
This shows a message after a 3-second delay. The second Sleep with no parameters waits indefinitely for a keypress. This creates a pause for user interaction.
CPU Usage Reduction
Sleep can reduce CPU usage in polling loops.
Dim done As Boolean = False While Not done ' Check for some condition If GetKeyState(27) Then ' ESC key pressed done = True End If Sleep 100 ' Reduce CPU usage by pausing between checks Wend Print "Loop exited"
This loop checks for the ESC key but includes a 100ms Sleep to prevent CPU overuse. Without Sleep, the loop would run as fast as possible, consuming significant CPU resources.
Precise Timing with Sleep
For more precise timing, Sleep can be combined with timers.
Dim startTime As Double = Timer Dim targetDelay As Integer = 2000 ' 2 seconds While (Timer - startTime) * 1000 < targetDelay ' Do some work here Print "Working..." Sleep 100 ' Small sleep to reduce CPU usage Wend Print "Precise 2 second delay completed"
This example shows how to create more precise timing than Sleep alone provides. The loop runs with small Sleeps while checking the actual elapsed time using the Timer function.
Best Practices
- Duration: Use appropriate sleep durations - too short wastes CPU, too long feels sluggish.
- Responsiveness: Avoid long sleeps in interactive applications.
- Alternatives: Consider timers or events instead of sleep for complex timing.
- Debugging: Temporarily reduce sleep times during development.
- Platform: Remember sleep duration accuracy varies across operating systems.
This tutorial covered the FreeBasic Sleep
keyword with practical
examples showing its usage in different scenarios.
Author
List all FreeBasic Tutorials.