Thread.Sleep Thread.Sleep Thread.Sleep Thread.Sleep Method

定義

在指定長度的時間內暫止目前的執行緒。Suspends the current thread for the specified amount of time.

多載

Sleep(Int32) Sleep(Int32) Sleep(Int32) Sleep(Int32)

在指定的毫秒數內暫止目前的執行緒。Suspends the current thread for the specified number of milliseconds.

Sleep(TimeSpan) Sleep(TimeSpan) Sleep(TimeSpan) Sleep(TimeSpan)

在指定長度的時間內暫止目前的執行緒。Suspends the current thread for the specified amount of time.

Sleep(Int32) Sleep(Int32) Sleep(Int32) Sleep(Int32)

在指定的毫秒數內暫止目前的執行緒。Suspends the current thread for the specified number of milliseconds.

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 Int32 Int32 Int32

暫止執行緒的毫秒數。The number of milliseconds for which the thread is suspended. 如果 millisecondsTimeout 引數的值為零,則執行緒會將其剩餘的時間配量讓與準備好要執行的任何同等優先權執行緒。If the value of the millisecondsTimeout argument is zero, the thread relinquishes the remainder of its time slice to any thread of equal priority that is ready to run. 如果沒有其他準備好要執行的同等優先權執行緒,則目前執行緒的執行不會暫停。If there are no other threads of equal priority that are ready to run, execution of the current thread is not suspended.

例外狀況

逾時值為負且不等於 InfiniteThe time-out value is negative and is not equal to Infinite.

範例

下列範例會使用 Sleep 方法來封鎖應用程式的主執行緒。The following example uses the Sleep method to block the application's main thread.

using namespace System;
using namespace System::Threading;

int 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.
 */
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.
 */
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.

備註

作業系統不會針對指定的時間長度,將執行緒排程為執行。The thread will not be scheduled for execution by the operating system for the amount of time specified. 這個方法會將執行緒的狀態變更為包含 WaitSleepJoinThis method changes the state of the thread to include WaitSleepJoin.

您可以指定 Timeout.Infinite,讓 millisecondsTimeout 參數無限期地暫停執行緒。You can specify Timeout.Infinite for the millisecondsTimeout parameter to suspend the thread indefinitely. 不過,我們建議您改為使用其他 System.Threading 類別(例如 MutexMonitorEventWaitHandleSemaphore)來同步處理執行緒或管理資源。However, we recommend that you use other System.Threading classes such as Mutex, Monitor, EventWaitHandle, or Semaphore instead to synchronize threads or manage resources.

以特定速率(稱為「時鐘解析度」)的系統時鐘刻度。The system clock ticks at a specific rate called the clock resolution. 實際的超時時間可能不是指定的超時時間,因為指定的超時時間將會調整以符合頻率刻度。The actual timeout might not be exactly the specified timeout, because the specified timeout will be adjusted to coincide with clock ticks. 如需有關時鐘解析和等候時間的詳細資訊,請參閱Sleep函式主題。For more information on clock resolution and the waiting time, see the Sleep function topic. 這個方法會從 Windows 系統 Api 呼叫Sleep 函數This method calls the Sleep function from the Windows system APIs.

這個方法不會執行標準的 COM 和 SendMessage 泵。This method does not perform standard COM and SendMessage pumping.

注意

如果您需要在具有 STAThreadAttribute 的執行緒上睡眠,但想要執行標準 COM 和 SendMessage 提取,請考慮使用 Join 方法的其中一個多載來指定逾時間隔。If you need to sleep on a thread that has STAThreadAttribute, but you want to perform standard COM and SendMessage pumping, consider using one of the overloads of the Join method that specifies a timeout interval.

Sleep(TimeSpan) Sleep(TimeSpan) Sleep(TimeSpan) Sleep(TimeSpan)

在指定長度的時間內暫止目前的執行緒。Suspends the current thread for the specified amount of time.

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 TimeSpan TimeSpan TimeSpan

暫止執行緒的時間長度。The amount of time for which the thread is suspended. 如果 millisecondsTimeout 引數的值為 Zero,則執行緒會將其剩餘的時間配量讓與準備好要執行的任何同等優先權執行緒。If the value of the millisecondsTimeout argument is Zero, the thread relinquishes the remainder of its time slice to any thread of equal priority that is ready to run. 如果沒有其他準備好要執行的同等優先權執行緒,則目前執行緒的執行不會暫停。If there are no other threads of equal priority that are ready to run, execution of the current thread is not suspended.

例外狀況

timeout 為負數且不等於 Infinite 毫秒,或大於 MaxValue 毫秒。The value of timeout is negative and is not equal to Infinite in milliseconds, or is greater than MaxValue milliseconds.

範例

下列範例會使用 Sleep(TimeSpan) 方法多載來封鎖應用程式的主執行緒五次,每次執行兩秒。The following example uses the Sleep(TimeSpan) method overload to block the application's main thread five times, for two seconds each time.

using namespace System;
using namespace System::Threading;

int main()
{
    TimeSpan interval = 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.
 */
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.
 */
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.

備註

作業系統不會針對指定的時間長度,將執行緒排程為執行。The thread will not be scheduled for execution by the operating system for the amount of time specified. 這個方法會將執行緒的狀態變更為包含 WaitSleepJoinThis method changes the state of the thread to include WaitSleepJoin.

您可以指定 Timeout.InfiniteTimeSpan,讓 timeout 參數無限期地暫停執行緒。You can specify Timeout.InfiniteTimeSpan for the timeout parameter to suspend the thread indefinitely. 不過,我們建議您改為使用其他 System.Threading 類別(例如 MutexMonitorEventWaitHandleSemaphore)來同步處理執行緒或管理資源。However, we recommend that you use other System.Threading classes such as Mutex, Monitor, EventWaitHandle, or Semaphore instead to synchronize threads or manage resources.

@No__t-0 的這個多載會使用 timeout 的完整毫秒總數。This overload of Sleep uses the total number of whole milliseconds in timeout. 捨棄小數毫秒。Fractional milliseconds are discarded.

這個方法不會執行標準的 COM 和 SendMessage 泵。This method does not perform standard COM and SendMessage pumping.

注意

如果您需要在具有 STAThreadAttribute 的執行緒上睡眠,但想要執行標準 COM 和 SendMessage 提取,請考慮使用 Join 方法的其中一個多載來指定逾時間隔。If you need to sleep on a thread that has STAThreadAttribute, but you want to perform standard COM and SendMessage pumping, consider using one of the overloads of the Join method that specifies a timeout interval.

適用於