public:
 static void Sleep(int millisecondsTimeout);
public static void Sleep(int millisecondsTimeout);
static member Sleep : int -> unit
Public Shared Sub Sleep (millisecondsTimeout As Integer)
// 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)
        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 重载之一。

static void Sleep(TimeSpan timeout);
public static void Sleep(TimeSpan timeout);
static member Sleep : TimeSpan -> unit
Public Shared Sub Sleep (timeout As TimeSpan)
// 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)
        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 重载之一。