Share via


Thread.Sleep 메서드

정의

지정된 시간 동안 현재 스레드를 일시 중단합니다.

오버로드

Sleep(Int32)

지정된 시간(밀리초) 동안 현재 스레드를 일시 중단합니다.

Sleep(TimeSpan)

지정된 시간 동안 현재 스레드를 일시 중단합니다.

Sleep(Int32)

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 인수의 값이 0이면 스레드는 실행할 준비가 된 우선 순위가 같은 스레드에 나머지 시간 간격을 내어 줍니다. 실행할 준비가 된 우선 순위가 같은 스레드가 없으면 현재 스레드의 실행이 일시 중단되지 않습니다.

예외

시간 제한 값이 음수이며 Infinite와 같지 않습니다.

예제

다음 예제에서는 Sleep 애플리케이션의 주 스레드를 차단 하는 방법입니다.

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.
 */
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하도록 스레드의 상태를 변경합니다.

매개 변수가 스레드를 millisecondsTimeout 무기한 일시 중단하도록 지정할 Timeout.Infinite 수 있습니다. 그러나 스레드를 동기화하거나 리소스를 관리하는 대신 , , EventWaitHandleMonitor또는 Semaphore 와 같은 Mutex다른 System.Threading 클래스를 사용하는 것이 좋습니다.

시스템 클록은 클록 해상도라는 특정 속도로 틱합니다. 지정된 시간 제한이 클록 틱과 일치하도록 조정되므로 실제 시간 제한은 지정된 시간 제한이 아닐 수 있습니다. 클록 해상도 및 대기 시간에 대한 자세한 내용은 Windows 시스템 API의 절전 모드 함수 를 참조하세요.

이 메서드는 표준 COM 및 SendMessage 펌핑을 수행하지 않습니다.

참고

가 있는 STAThreadAttribute스레드에서 절전 모드로 이동해야 하지만 표준 COM 및 SendMessage 펌핑을 수행하려는 경우 시간 제한 간격을 지정하는 메서드의 Join 오버로드 중 하나를 사용하는 것이 좋습니다.

적용 대상

Sleep(TimeSpan)

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

스레드가 일시 중단되는 시간입니다. timeout 인수의 값이 Zero이면 스레드는 실행할 준비가 된 우선 순위가 같은 스레드에 나머지 시간 간격을 내어 줍니다. 실행할 준비가 된 우선 순위가 같은 스레드가 없으면 현재 스레드의 실행이 일시 중단되지 않습니다.

예외

값이 음수 timeout 이고 밀리초 단위가 아니 Infinite 거나 Int32.MaxValue 밀리초보다 큽니다.

예제

다음 예제에서는 Sleep(TimeSpan) 메서드 오버 로드를 2 초 동안 때마다 다섯 번 애플리케이션의 주 스레드를 차단 합니다.

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.
 */
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 무기한 일시 중단하도록 지정할 Timeout.InfiniteTimeSpan 수 있습니다. 그러나 스레드를 동기화하거나 리소스를 관리하는 대신 , , EventWaitHandleMonitor또는 Semaphore 와 같은 Mutex다른 System.Threading 클래스를 사용하는 것이 좋습니다.

이 오버로드는 Sleep 의 전체 밀리초 timeout수를 사용합니다. 소수 밀리초는 무시됩니다.

이 메서드는 표준 COM 및 SendMessage 펌핑을 수행하지 않습니다.

참고

가 있는 STAThreadAttribute스레드에서 절전 모드로 이동해야 하지만 표준 COM 및 SendMessage 펌핑을 수행하려는 경우 시간 제한 간격을 지정하는 메서드의 Join 오버로드 중 하나를 사용하는 것이 좋습니다.

적용 대상