Blocks the current thread for the specified number of milliseconds.
Overload List
Suspends the current thread for a specified time.
Supported by the .NET Compact Framework.
[Visual Basic] Overloads Public Shared Sub Sleep(Integer)
[C#] public static void Sleep(int);
[C++] public: static void Sleep(int);
[JScript] public static function Sleep(int);
Blocks the current thread for a specified time.
[Visual Basic] Overloads Public Shared Sub Sleep(TimeSpan)
[C#] public static void Sleep(TimeSpan);
[C++] public: static void Sleep(TimeSpan);
[JScript] public static function Sleep(TimeSpan);
Example
[Visual Basic, C#, C++] The following code example demonstrates how to use a TimeSpan value with the Sleep method.
[Visual Basic, C#, C++] Note This example shows how to use one of the overloaded versions of Sleep. For other examples that might be available, see the individual overload topics.
[Visual Basic]
Imports System
Imports System.Threading
Public Class Test
Shared waitTime As New TimeSpan(0, 0, 1)
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
[C#]
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);
}
}
[C++]
#using <mscorlib.dll>
using namespace System;
using namespace System::Threading;
static TimeSpan waitTime = TimeSpan(0, 0, 1);
__gc class Test
{
public:
static void Work()
{
Thread::Sleep(waitTime);
}
};
void main()
{
Thread* newThread = new Thread(new ThreadStart(0, Test::Work));
newThread->Start();
if(newThread->Join(waitTime + waitTime))
{
Console::WriteLine(S"New thread terminated.");
}
else
{
Console::WriteLine(S"Join timed out.");
}
}
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
See Also
Thread Class | Thread Members | System.Threading Namespace