Thread.Sleep 方法

定义

暂停当前线程的指定时间量。

重载

名称 说明
Sleep(Int32)

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

Sleep(TimeSpan)

暂停当前线程的指定时间量。

Sleep(Int32)

Source:
Thread.cs
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 该方法阻止应用程序的主线程。

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.InfinitemillisecondsTimeout参数无限期挂起线程。 但是,我们建议你使用其他System.Threading类,例如MutexMonitorEventWaitHandleSemaphore改为同步线程或管理资源。

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

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

注释

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

适用于

Sleep(TimeSpan)

Source:
Thread.cs
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

线程挂起的时间量。 如果参数的值 timeoutZero,则线程将时间切片的其余部分放弃到任何已准备好运行的优先级相等的线程。 如果没有其他优先级相等的线程可供运行,则不会挂起当前线程的执行。

例外

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

示例

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

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.InfiniteTimeSpantimeout参数无限期挂起线程。 但是,我们建议你使用其他System.Threading类,例如MutexMonitorEventWaitHandleSemaphore改为同步线程或管理资源。

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

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

注释

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

适用于