Export (0) Print
Expand All
0 out of 2 rated this helpful - Rate this topic

Thread.Sleep Method (TimeSpan)

Blocks the current thread for a specified time.

Namespace: System.Threading
Assembly: mscorlib (in mscorlib.dll)

public static void Sleep (
	TimeSpan timeout
)
public static void Sleep (
	TimeSpan timeout
)
public static function Sleep (
	timeout : TimeSpan
)
Not applicable.

Parameters

timeout

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.

Exception typeCondition

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.

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

import System.*;
import System.Threading.*;
import System.Threading.Thread;

class Test
{
    private static TimeSpan waitTime = new TimeSpan(0, 0, 1);

    public static void main(String[] args)
    {
        Thread newThread = new Thread(new ThreadStart(Work));

        newThread.Start();
        if (newThread.Join((waitTime.Add(waitTime)))) {
            Console.WriteLine("New thread terminated.");
        }
        else {
            Console.WriteLine("Join timed out.");
        }
    } //main

    static void Work()
    {
        Thread.Sleep(waitTime);
    } //Work
} //Test

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.