Thread.Sleep 方法

定义

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

重载

Sleep(Int32)

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

Sleep(TimeSpan)

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

Sleep(Int32)

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

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

C#
public static void Sleep(int millisecondsTimeout);

参数

millisecondsTimeout
Int32

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

例外

超时值为负且不等于 Infinite

示例

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

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

注解

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

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

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

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

备注

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

适用于

.NET 10 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Sleep(TimeSpan)

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

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

C#
public static void Sleep(TimeSpan timeout);

参数

timeout
TimeSpan

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

例外

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

示例

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

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

注解

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

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

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

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

备注

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

适用于

.NET 10 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1