Thread.Sleep 方法

定义

将当前线程挂起指定的时间。

重载

Sleep(Int32)

将当前线程挂起指定的毫秒数。

Sleep(TimeSpan)

将当前线程挂起指定的时间。

Sleep(Int32)

Source:
Thread.cs
Source:
Thread.cs
Source:
Thread.cs
Source:
Thread.cs

将当前线程挂起指定的毫秒数。

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

挂起线程的毫秒数。 如果 millisecondsTimeout 参数的值为零,则该线程会将其时间片的剩余部分让给任何已经准备好运行的、具有同等优先级的线程。 如果没有其他已经准备好运行的、具有同等优先级的线程,则不会挂起当前线程的执行。

例外

超时值为负且不等于 Infinite

示例

以下示例使用 Sleep 方法来阻止应用程序的main线程。

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

for _ = 0 to 4 do
    printfn "Sleep for 2 seconds."
    Thread.Sleep 2000

printfn "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.

注解

操作系统不会在指定的时间量内计划线程的执行。 此方法将线程的状态更改为包含 WaitSleepJoin

可以指定 Timeout.Infinite 参数 millisecondsTimeout 以无限期挂起线程。 但是,我们建议改用其他 System.Threading 类(如 MutexMonitorEventWaitHandleSemaphore )来同步线程或管理资源。

系统时钟以称为时钟分辨率的特定速率计时。 实际超时可能不完全是指定的超时,因为指定的超时将调整为与时钟周期一致。 有关时钟分辨率和等待时间的详细信息,请参阅 Windows 系统 API 中的 睡眠函数

此方法不执行标准 COM 和 SendMessage 抽水。

注意

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

适用于

Sleep(TimeSpan)

Source:
Thread.cs
Source:
Thread.cs
Source:
Thread.cs
Source:
Thread.cs

将当前线程挂起指定的时间。

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

挂起线程的时间量。 如果 timeout 参数的值为 Zero,则该线程会将其时间片的剩余部分让给任何已经准备好运行的、具有同等优先级的线程。 如果没有其他已经准备好运行的、具有同等优先级的线程,则不会挂起当前线程的执行。

例外

timeout 值为负,不等于 Infinite 以毫秒为单位,或大于 Int32.MaxValue 毫秒。

示例

以下示例使用 Sleep(TimeSpan) 方法重载来阻止应用程序的main线程五次,每次两秒。

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

let interval = TimeSpan(0, 0, 2)

for _ = 0 to 4 do
    printfn "Sleep for 2 seconds."
    Thread.Sleep interval

printfn "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.

注解

操作系统不会在指定的时间量内计划线程的执行。 此方法将线程的状态更改为包含 WaitSleepJoin

可以指定 Timeout.InfiniteTimeSpan 参数 timeout 以无限期挂起线程。 但是,我们建议改用其他 System.Threading 类(如 MutexMonitorEventWaitHandleSemaphore )来同步线程或管理资源。

Sleep 此重载使用 中的 timeout总毫秒数。 丢弃小数毫秒。

此方法不执行标准 COM 和 SendMessage 抽水。

注意

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

适用于