C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Calls: Sleep(0) pauses the program for zero milliseconds. Sleep(5000) pauses for five seconds. Sleep(1000) pauses for one second.
SpinWait: We invoke SpinWait. This subroutine results in a lot of CPU usage based on the large integer passed to it.
Tip: SpinWait is the opposite of a Sleep call. It pegs the CPU at 100%.
StopwatchVB.NET program that uses the Sleep method
Imports System.Threading
Module Module1
Sub Main()
' Create a Stopwatch and sleep for zero milliseconds.
Dim stopwatch As Stopwatch = stopwatch.StartNew
Thread.Sleep(0)
stopwatch.Stop()
' Write the current time.
Console.WriteLine(stopwatch.ElapsedMilliseconds)
Console.WriteLine(DateTime.Now.ToLongTimeString)
' Start a new Stopwatch.
stopwatch = stopwatch.StartNew
Thread.Sleep(5000)
stopwatch.Stop()
Console.WriteLine(stopwatch.ElapsedMilliseconds)
Console.WriteLine(DateTime.Now.ToLongTimeString)
' Start a new Stopwatch.
stopwatch = stopwatch.StartNew
Thread.Sleep(1000)
stopwatch.Stop()
Console.WriteLine(stopwatch.ElapsedMilliseconds)
' Start a new Stopwatch and use SpinWait.
stopwatch = stopwatch.StartNew
Thread.SpinWait(1000000000)
stopwatch.Stop()
Console.WriteLine(stopwatch.ElapsedMilliseconds)
End Sub
End Module
Output
0
9:36:02 AM
4999
9:36:07 AM
999
3128