Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
Thread Class
Thread Methods
Sleep Method
 Sleep Method (TimeSpan)

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
Thread..::.Sleep Method (TimeSpan)

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 _
)
Visual Basic (Usage)
Dim timeout As TimeSpan

Thread.Sleep(timeout)
C#
public static void Sleep(
    TimeSpan timeout
)
Visual C++
public:
static void Sleep(
    TimeSpan timeout
)
JScript
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.
ExceptionCondition
ArgumentOutOfRangeException

The value of timeout is negative and is not equal to Timeout..::.Infinite in milliseconds, or is greater than Int32..::.MaxValue milliseconds.

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.

NoteNote:

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.

Visual Basic
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

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);
    }
}

Visual C++
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
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker