Thread.Sleep 方法

定義

暫停目前執行緒指定時間。

多載

名稱 Description
Sleep(Int32)

暫停目前執行緒指定毫秒數。

Sleep(TimeSpan)

暫停目前執行緒指定時間。

Sleep(Int32)

來源:
Thread.cs
來源:
Thread.cs
來源:
Thread.cs
來源:
Thread.cs
來源:
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 類別,如 MutexMonitorEventWaitHandle,或 Semaphore 是用來同步執行緒或管理資源。

系統時鐘以特定速率滴答,稱為時鐘解析度。 實際的逾時可能不完全符合指定的逾時,因為指定的逾時會調整以配合時鐘週期。 欲了解更多時鐘解析度與等待時間,請參閱Windows系統 API 中的 Sleep function

此方法不執行標準的 COM 與 SendMessage 抽取。

Note

如果你需要在執行緒 STAThreadAttribute中睡眠,但你想執行標準的 COM 和 SendMessage 泵浦,可以考慮使用該方法中 Join 指定逾時間隔的超載功能。

適用於

Sleep(TimeSpan)

來源:
Thread.cs
來源:
Thread.cs
來源:
Thread.cs
來源:
Thread.cs
來源:
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) 方法過載來阻擋應用程式主執行緒五次,每次兩秒。

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 類別,如 MutexMonitorEventWaitHandle,或 Semaphore 是用來同步執行緒或管理資源。

此過載Sleep使用了整毫秒數的總數。timeout 分毫秒則被捨棄。

此方法不執行標準的 COM 與 SendMessage 抽取。

Note

如果你需要在執行緒 STAThreadAttribute中睡眠,但你想執行標準的 COM 和 SendMessage 泵浦,可以考慮使用該方法中 Join 指定逾時間隔的超載功能。

適用於