Thread.Sleep Method (TimeSpan)
Suspends the current thread for the specified amount of time.
Assembly: mscorlib (in mscorlib.dll)
- Type: System.TimeSpan
The amount of time for which the thread is suspended. If the value of the millisecondsTimeout argument is TimeSpan.Zero, the thread relinquishes the remainder of its time slice to any thread of equal priority that is ready to run. If there are no other threads of equal priority that are ready to run, execution of the current thread is not suspended.
The thread will not be scheduled for execution by the operating system for the amount of time specified. This method changes the state of the thread to include WaitSleepJoin.
You can specify Timeout.InfiniteTimeSpan for the timeout parameter to suspend the thread indefinitely. However, we recommend that you use other System.Threading classes such as Mutex, Monitor, EventWaitHandle, or Semaphore instead to sychronize threads or manage resources.
This overload of Sleep uses the total number of whole milliseconds in timeout. Fractional milliseconds are discarded.
This method does not perform standard COM and SendMessage pumping.
The following example uses the method overload to block the application's main thread five times, for two seconds each time.
Imports System.Threading Class Example Shared Sub Main() Dim interval As New TimeSpan(0, 0, 2) For i As Integer = 0 To 4 Console.WriteLine("Sleep for 2 seconds.") Thread.Sleep(interval) Next Console.WriteLine("Main thread exits.") End Sub End Class ' This example produces the following output: ' 'Sleep for 2 seconds. 'Sleep for 2 seconds. 'Sleep for 2 seconds. 'Sleep for 2 seconds. 'Sleep for 2 seconds. 'Main thread exits.
Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.