Thread.Sleep Thread.Sleep Thread.Sleep Thread.Sleep Method

定义

将当前线程挂起指定的时间。Suspends the current thread for the specified amount of time.

重载

Sleep(Int32) Sleep(Int32) Sleep(Int32) Sleep(Int32)

将当前线程挂起指定的毫秒数。Suspends the current thread for the specified number of milliseconds.

Sleep(TimeSpan) Sleep(TimeSpan) Sleep(TimeSpan) Sleep(TimeSpan)

将当前线程挂起指定的时间。Suspends the current thread for the specified amount of time.

Sleep(Int32) Sleep(Int32) Sleep(Int32) Sleep(Int32)

将当前线程挂起指定的毫秒数。Suspends the current thread for the specified number of milliseconds.

public:
 static void Sleep(int millisecondsTimeout);
public static void Sleep (int millisecondsTimeout);
static member Sleep : int -> unit
Public Shared Sub Sleep (millisecondsTimeout As Integer)

参数

millisecondsTimeout
Int32 Int32 Int32 Int32

挂起线程的毫秒数。The number of milliseconds for which the thread is suspended. 如果 millisecondsTimeout 参数的值为零,则该线程会将其时间片的剩余部分让给任何已经准备好运行的、具有同等优先级的线程。If the value of the millisecondsTimeout argument is 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.

异常

超时值为负且不等于 InfiniteThe time-out value is negative and is not equal to Infinite.

示例

下面的示例使用Sleep方法来阻止应用程序的主线程。The following example uses the Sleep method to block the application's main thread.

using namespace System;
using namespace System::Threading;

int main()
{
    for (int i = 0; i < 5; i++)
    {
        Console::WriteLine("Sleep for 2 seconds.");
        Thread::Sleep(2000);
    }

    Console::WriteLine("Main thread exits.");
}

/* 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.
 */
using System;
using System.Threading;

class Example
{
    static void Main()
    {
        for (int i = 0; i < 5; i++)
        {
            Console.WriteLine("Sleep for 2 seconds.");
            Thread.Sleep(2000);
        }

        Console.WriteLine("Main thread exits.");
    }
}

/* 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.
 */
Imports System.Threading

Class Example

    Shared Sub Main()

        For i As Integer = 0 To 4
            Console.WriteLine("Sleep for 2 seconds.")
            Thread.Sleep(2000)
        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.

注解

在指定的时间内, 将不会将线程计划为由操作系统执行。The thread will not be scheduled for execution by the operating system for the amount of time specified. 此方法更改要包含WaitSleepJoin的线程的状态。This method changes the state of the thread to include WaitSleepJoin.

Timeout.Infinite 可以millisecondsTimeout为参数指定, 以无限期挂起线程。You can specify Timeout.Infinite for the millisecondsTimeout parameter to suspend the thread indefinitely. 但是, 我们建议你使用其他System.ThreadingMutex(例如、 MonitorEventWaitHandleSemaphore ) 来同步线程或管理资源。However, we recommend that you use other System.Threading classes such as Mutex, Monitor, EventWaitHandle, or Semaphore instead to synchronize threads or manage resources.

系统时钟计时周期, 按特定速率称为时钟解析。The system clock ticks at a specific rate called the clock resolution. 实际超时可能不是精确到指定的超时值, 因为将调整指定的超时值, 使其与时钟计时周期一致。The actual timeout might not be exactly the specified timeout, because the specified timeout will be adjusted to coincide with clock ticks. 有关时钟解析和等待时间的详细信息, 请参阅睡眠函数主题。For more information on clock resolution and the waiting time, see the Sleep function topic. 此方法从 Windows 系统 Api 调用睡眠函数This method calls the Sleep function from the Windows system APIs.

此方法不执行标准的 COM 和 SendMessage 泵。This method does not perform standard COM and SendMessage pumping.

备注

如果需要在具有STAThreadAttribute的线程上睡眠, 但想要执行标准的 COM 和 SendMessage 泵泵, 请考虑使用指定超时间隔的Join方法重载之一。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.

Sleep(TimeSpan) Sleep(TimeSpan) Sleep(TimeSpan) Sleep(TimeSpan)

将当前线程挂起指定的时间。Suspends the current thread for the specified amount of time.

public:
 static void Sleep(TimeSpan timeout);
public static void Sleep (TimeSpan timeout);
static member Sleep : TimeSpan -> unit
Public Shared Sub Sleep (timeout As TimeSpan)

参数

timeout
TimeSpan TimeSpan TimeSpan TimeSpan

挂起线程的时间量。The amount of time for which the thread is suspended. 如果 millisecondsTimeout 参数的值为 Zero,则该线程会将其时间片的剩余部分让给任何已经准备好运行的、具有同等优先级的线程。If the value of the millisecondsTimeout argument is 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.

异常

timeout 的值为负数,且不等于 Infinite(以毫秒为单位),或大于 MaxValue 毫秒。The value of timeout is negative and is not equal to Infinite in milliseconds, or is greater than MaxValue milliseconds.

示例

下面的示例使用Sleep(TimeSpan)方法重载来阻止应用程序的主线程5次, 每次等待两秒钟。The following example uses the Sleep(TimeSpan) method overload to block the application's main thread five times, for two seconds each time.

using namespace System;
using namespace System::Threading;

int main()
{
    TimeSpan interval = TimeSpan(0, 0, 2);

    for (int i = 0; i < 5; i++)
    {
        Console::WriteLine("Sleep for 2 seconds.");
        Thread::Sleep(interval);
    }

    Console::WriteLine("Main thread exits.");
}

/* 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.
 */
using System;
using System.Threading;

class Example
{
    static void Main()
    {
        TimeSpan interval = new TimeSpan(0, 0, 2);

        for (int i = 0; i < 5; i++)
        {
            Console.WriteLine("Sleep for 2 seconds.");
            Thread.Sleep(interval);
        }

        Console.WriteLine("Main thread exits.");
    }
}

/* 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.
 */
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.

注解

在指定的时间内, 将不会将线程计划为由操作系统执行。The thread will not be scheduled for execution by the operating system for the amount of time specified. 此方法更改要包含WaitSleepJoin的线程的状态。This method changes the state of the thread to include WaitSleepJoin.

Timeout.InfiniteTimeSpan 可以timeout为参数指定, 以无限期挂起线程。You can specify Timeout.InfiniteTimeSpan for the timeout parameter to suspend the thread indefinitely. 但是, 我们建议你使用其他System.ThreadingMutex(例如、 MonitorEventWaitHandleSemaphore ) 来同步线程或管理资源。However, we recommend that you use other System.Threading classes such as Mutex, Monitor, EventWaitHandle, or Semaphore instead to synchronize threads or manage resources.

Sleep此重载使用中timeout的总毫秒数。This overload of Sleep uses the total number of whole milliseconds in timeout. 丢弃毫秒的小数部分。Fractional milliseconds are discarded.

此方法不执行标准的 COM 和 SendMessage 泵。This method does not perform standard COM and SendMessage pumping.

备注

如果需要在具有STAThreadAttribute的线程上睡眠, 但想要执行标准的 COM 和 SendMessage 泵泵, 请考虑使用指定超时间隔的Join方法重载之一。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.

适用于