Blocks the current thread for a specified time.
Namespace:
System.Threading
Assembly:
mscorlib (in mscorlib.dll)
Visual Basic (Declaration)
Public Shared Sub Sleep ( _
timeout As TimeSpan _
)
Dim timeout As TimeSpan
Thread.Sleep(timeout)
public static void Sleep(
TimeSpan timeout
)
public:
static void Sleep(
TimeSpan timeout
)
public static function Sleep(
timeout : TimeSpan
)
Parameters
- timeout
- Type: System..::.TimeSpan
A TimeSpan set to the amount of time for which the thread is blocked. Specify zero to indicate that this thread should be suspended to allow other waiting threads to execute. Specify Timeout..::.Infinite to block the thread indefinitely.
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.
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.
Note: |
|---|
If you need to sleep on a thread that has STAThreadAttribute, but you want to perform standard COM and SendMessage pumping, consider using one of the overloads of the Join method that specifies a timeout interval. |
The following code example demonstrates how to use a TimeSpan value with the Sleep method.
Imports System
Imports System.Threading
Public Class Test
Shared waitTime As New TimeSpan(0, 0, 1)
<MTAThread> _
Shared Sub Main()
Dim newThread As New Thread(AddressOf Work)
newThread.Start()
If newThread.Join( _
TimeSpan.op_Addition(waitTime, waitTime)) Then
Console.WriteLine("New thread terminated.")
Else
Console.WriteLine("Join timed out.")
End If
End Sub
Shared Sub Work()
Thread.Sleep(waitTime)
End Sub
End Class
using System;
using System.Threading;
class Test
{
static TimeSpan waitTime = new TimeSpan(0, 0, 1);
public static void Main()
{
Thread newThread =
new Thread(new ThreadStart(Work));
newThread.Start();
if(newThread.Join(waitTime + waitTime))
{
Console.WriteLine("New thread terminated.");
}
else
{
Console.WriteLine("Join timed out.");
}
}
static void Work()
{
Thread.Sleep(waitTime);
}
}
using namespace System;
using namespace System::Threading;
static TimeSpan waitTime = TimeSpan(0,0,1);
ref class Test
{
public:
static void Work()
{
Thread::Sleep( waitTime );
}
};
int main()
{
Thread^ newThread = gcnew Thread( gcnew ThreadStart( Test::Work ) );
newThread->Start();
if ( newThread->Join( waitTime + waitTime ) )
{
Console::WriteLine( "New thread terminated." );
}
else
{
Console::WriteLine( "Join timed out." );
}
}
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
.NET Framework
Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
Reference