AutoResetEvent 클래스

정의

신호를 받으면 하나의 대기 스레드를 해제한 다음 자동으로 다시 설정되는 스레드 동기화 이벤트를 나타냅니다. 이 클래스는 상속될 수 없습니다.

public ref class AutoResetEvent sealed : System::Threading::EventWaitHandle
public ref class AutoResetEvent sealed : System::Threading::WaitHandle
public sealed class AutoResetEvent : System.Threading.EventWaitHandle
public sealed class AutoResetEvent : System.Threading.WaitHandle
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class AutoResetEvent : System.Threading.EventWaitHandle
type AutoResetEvent = class
    inherit EventWaitHandle
type AutoResetEvent = class
    inherit WaitHandle
[<System.Runtime.InteropServices.ComVisible(true)>]
type AutoResetEvent = class
    inherit EventWaitHandle
Public NotInheritable Class AutoResetEvent
Inherits EventWaitHandle
Public NotInheritable Class AutoResetEvent
Inherits WaitHandle
상속
상속
상속
특성

예제

다음 예제에서는 를 사용하여 AutoResetEvent 사용자가 Enter 키를 누를 때마다 메서드(기본 클래스)를 호출 Set 하여 한 번에 하나의 스레드를 해제하는 방법을 보여 줍니다. 이 예제에서는 신호된 상태에서 생성된 을 AutoResetEvent 대기하는 세 개의 스레드를 시작합니다. 가 이미 신호를 받은 상태이기 때문에 AutoResetEvent 첫 번째 스레드가 즉시 해제됩니다. 그러면 가 신호되지 않은 상태로 다시 설정 AutoResetEvent 되므로 후속 스레드가 차단됩니다. 차단된 스레드는 사용자가 Enter 키를 눌러 한 번에 하나씩 해제할 때까지 해제되지 않습니다.

스레드가 첫 번째 AutoResetEvent에서 해제된 후 신호가 없는 상태에서 만들어진 다른 AutoResetEvent 스레드를 기다립니다. 세 스레드가 모두 차단되므로 메서드를 Set 모두 해제하려면 메서드를 세 번 호출해야 합니다.

using namespace System;
using namespace System::Threading;

ref class Example
{
private:
    static AutoResetEvent^ event_1 = gcnew AutoResetEvent(true);
    static AutoResetEvent^ event_2 = gcnew AutoResetEvent(false);

    static void ThreadProc()
    {
        String^ name = Thread::CurrentThread->Name;

        Console::WriteLine("{0} waits on AutoResetEvent #1.", name);
        event_1->WaitOne();
        Console::WriteLine("{0} is released from AutoResetEvent #1.", name);

        Console::WriteLine("{0} waits on AutoResetEvent #2.", name);
        event_2->WaitOne();
        Console::WriteLine("{0} is released from AutoResetEvent #2.", name);

        Console::WriteLine("{0} ends.", name);
    }

public:
    static void Demo()
    {
        Console::WriteLine("Press Enter to create three threads and start them.\r\n" +
                           "The threads wait on AutoResetEvent #1, which was created\r\n" +
                           "in the signaled state, so the first thread is released.\r\n" +
                           "This puts AutoResetEvent #1 into the unsignaled state.");
        Console::ReadLine();
            
        for (int i = 1; i < 4; i++)
        {
            Thread^ t = gcnew Thread(gcnew ThreadStart(&ThreadProc));
            t->Name = "Thread_" + i;
            t->Start();
        }
        Thread::Sleep(250);

        for (int i = 0; i < 2; i++)
        {
            Console::WriteLine("Press Enter to release another thread.");
            Console::ReadLine();
            event_1->Set();
            Thread::Sleep(250);
        }

        Console::WriteLine("\r\nAll threads are now waiting on AutoResetEvent #2.");
        for (int i = 0; i < 3; i++)
        {
            Console::WriteLine("Press Enter to release a thread.");
            Console::ReadLine();
            event_2->Set();
            Thread::Sleep(250);
        }

        // Visual Studio: Uncomment the following line.
        //Console::Readline();
    }
};

void main()
{
    Example::Demo();
}

/* This example produces output similar to the following:

Press Enter to create three threads and start them.
The threads wait on AutoResetEvent #1, which was created
in the signaled state, so the first thread is released.
This puts AutoResetEvent #1 into the unsignaled state.

Thread_1 waits on AutoResetEvent #1.
Thread_1 is released from AutoResetEvent #1.
Thread_1 waits on AutoResetEvent #2.
Thread_3 waits on AutoResetEvent #1.
Thread_2 waits on AutoResetEvent #1.
Press Enter to release another thread.

Thread_3 is released from AutoResetEvent #1.
Thread_3 waits on AutoResetEvent #2.
Press Enter to release another thread.

Thread_2 is released from AutoResetEvent #1.
Thread_2 waits on AutoResetEvent #2.

All threads are now waiting on AutoResetEvent #2.
Press Enter to release a thread.

Thread_2 is released from AutoResetEvent #2.
Thread_2 ends.
Press Enter to release a thread.

Thread_1 is released from AutoResetEvent #2.
Thread_1 ends.
Press Enter to release a thread.

Thread_3 is released from AutoResetEvent #2.
Thread_3 ends.
 */
using System;
using System.Threading;

// Visual Studio: Replace the default class in a Console project with 
//                the following class.
class Example
{
    private static AutoResetEvent event_1 = new AutoResetEvent(true);
    private static AutoResetEvent event_2 = new AutoResetEvent(false);

    static void Main()
    {
        Console.WriteLine("Press Enter to create three threads and start them.\r\n" +
                          "The threads wait on AutoResetEvent #1, which was created\r\n" +
                          "in the signaled state, so the first thread is released.\r\n" +
                          "This puts AutoResetEvent #1 into the unsignaled state.");
        Console.ReadLine();
            
        for (int i = 1; i < 4; i++)
        {
            Thread t = new Thread(ThreadProc);
            t.Name = "Thread_" + i;
            t.Start();
        }
        Thread.Sleep(250);

        for (int i = 0; i < 2; i++)
        {
            Console.WriteLine("Press Enter to release another thread.");
            Console.ReadLine();
            event_1.Set();
            Thread.Sleep(250);
        }

        Console.WriteLine("\r\nAll threads are now waiting on AutoResetEvent #2.");
        for (int i = 0; i < 3; i++)
        {
            Console.WriteLine("Press Enter to release a thread.");
            Console.ReadLine();
            event_2.Set();
            Thread.Sleep(250);
        }

        // Visual Studio: Uncomment the following line.
        //Console.Readline();
    }

    static void ThreadProc()
    {
        string name = Thread.CurrentThread.Name;

        Console.WriteLine("{0} waits on AutoResetEvent #1.", name);
        event_1.WaitOne();
        Console.WriteLine("{0} is released from AutoResetEvent #1.", name);

        Console.WriteLine("{0} waits on AutoResetEvent #2.", name);
        event_2.WaitOne();
        Console.WriteLine("{0} is released from AutoResetEvent #2.", name);

        Console.WriteLine("{0} ends.", name);
    }
}

/* This example produces output similar to the following:

Press Enter to create three threads and start them.
The threads wait on AutoResetEvent #1, which was created
in the signaled state, so the first thread is released.
This puts AutoResetEvent #1 into the unsignaled state.

Thread_1 waits on AutoResetEvent #1.
Thread_1 is released from AutoResetEvent #1.
Thread_1 waits on AutoResetEvent #2.
Thread_3 waits on AutoResetEvent #1.
Thread_2 waits on AutoResetEvent #1.
Press Enter to release another thread.

Thread_3 is released from AutoResetEvent #1.
Thread_3 waits on AutoResetEvent #2.
Press Enter to release another thread.

Thread_2 is released from AutoResetEvent #1.
Thread_2 waits on AutoResetEvent #2.

All threads are now waiting on AutoResetEvent #2.
Press Enter to release a thread.

Thread_2 is released from AutoResetEvent #2.
Thread_2 ends.
Press Enter to release a thread.

Thread_1 is released from AutoResetEvent #2.
Thread_1 ends.
Press Enter to release a thread.

Thread_3 is released from AutoResetEvent #2.
Thread_3 ends.
 */
Imports System.Threading

' Visual Studio: Replace the default class in a Console project with 
'                the following class.
Class Example

    Private Shared event_1 As New AutoResetEvent(True)
    Private Shared event_2 As New AutoResetEvent(False)

    <MTAThread()> _
    Shared Sub Main()
    
        Console.WriteLine("Press Enter to create three threads and start them." & vbCrLf & _
                          "The threads wait on AutoResetEvent #1, which was created" & vbCrLf & _
                          "in the signaled state, so the first thread is released." & vbCrLf & _
                          "This puts AutoResetEvent #1 into the unsignaled state.")
        Console.ReadLine()
            
        For i As Integer = 1 To 3
            Dim t As New Thread(AddressOf ThreadProc)
            t.Name = "Thread_" & i
            t.Start()
        Next
        Thread.Sleep(250)

        For i As Integer = 1 To 2
            Console.WriteLine("Press Enter to release another thread.")
            Console.ReadLine()

            event_1.Set()
            Thread.Sleep(250)
        Next

        Console.WriteLine(vbCrLf & "All threads are now waiting on AutoResetEvent #2.")
        For i As Integer = 1 To 3
            Console.WriteLine("Press Enter to release a thread.")
            Console.ReadLine()

            event_2.Set()
            Thread.Sleep(250)
        Next

        ' Visual Studio: Uncomment the following line.
        'Console.Readline()
    End Sub

    Shared Sub ThreadProc()
    
        Dim name As String = Thread.CurrentThread.Name

        Console.WriteLine("{0} waits on AutoResetEvent #1.", name)
        event_1.WaitOne()
        Console.WriteLine("{0} is released from AutoResetEvent #1.", name)

        Console.WriteLine("{0} waits on AutoResetEvent #2.", name)
        event_2.WaitOne()
        Console.WriteLine("{0} is released from AutoResetEvent #2.", name)

        Console.WriteLine("{0} ends.", name)
    End Sub
End Class

' This example produces output similar to the following:
'
'Press Enter to create three threads and start them.
'The threads wait on AutoResetEvent #1, which was created
'in the signaled state, so the first thread is released.
'This puts AutoResetEvent #1 into the unsignaled state.
'
'Thread_1 waits on AutoResetEvent #1.
'Thread_1 is released from AutoResetEvent #1.
'Thread_1 waits on AutoResetEvent #2.
'Thread_3 waits on AutoResetEvent #1.
'Thread_2 waits on AutoResetEvent #1.
'Press Enter to release another thread.
'
'Thread_3 is released from AutoResetEvent #1.
'Thread_3 waits on AutoResetEvent #2.
'Press Enter to release another thread.
'
'Thread_2 is released from AutoResetEvent #1.
'Thread_2 waits on AutoResetEvent #2.
'
'All threads are now waiting on AutoResetEvent #2.
'Press Enter to release a thread.
'
'Thread_2 is released from AutoResetEvent #2.
'Thread_2 ends.
'Press Enter to release a thread.
'
'Thread_1 is released from AutoResetEvent #2.
'Thread_1 ends.
'Press Enter to release a thread.
'
'Thread_3 is released from AutoResetEvent #2.
'Thread_3 ends.

설명

스레드 상호 작용(또는 스레드 신호)에 , ManualResetEventEventWaitHandle 를 사용합니다AutoResetEvent. 자세한 내용은 스레드 상호 작용을 참조하세요.

스레드는 AutoResetEvent.WaitOne을 호출하여 신호를 기다립니다. 가 AutoResetEvent 신호가 없는 상태이면 AutoResetEvent.Set 가 호출될 때까지 스레드가 차단됩니다. 대기 스레드를 해제하는 신호를 호출 Set 합니다 AutoResetEvent . AutoResetEvent 는 가 호출되거나 단일 대기 스레드가 해제될 때까지 Reset 신호를 유지하며, 이때 자동으로 신호가 없는 상태로 돌아갑니다.

가 신호된 상태로 전환될 때 AutoResetEvent 대기 중인 스레드가 없는 경우 스레드가 신호를 관찰할 때까지(를 호출 WaitOne하여) 상태가 신호로 유지됩니다. 해당 스레드는 차단되지 않습니다. 는 AutoResetEvent 스레드를 즉시 해제하고 신호가 없는 상태로 돌아갑니다.

중요

메서드에 대한 모든 호출이 스레드를 Set 해제한다는 보장은 없습니다. 두 호출이 너무 가깝기 때문에 스레드가 해제되기 전에 두 번째 호출이 발생하면 하나의 스레드만 해제됩니다. 마치 두 번째 호출이 발생하지 않은 것처럼 말입니다. Set 또한 대기 중인 스레드가 없고 가 AutoResetEvent 이미 신호를 받으면 이 호출에 영향을 주지 않습니다.

초기 상태가 AutoResetEvent 신호를 받으면 생성자에 true 부울 값을 전달하여 의 초기 상태를 제어할 수 있습니다 false .

AutoResetEvent는 및 WaitAny 메서드와 함께 staticWaitAll 사용할 수도 있습니다.

AutoResetEvent 파생 되는 EventWaitHandle 클래스입니다. AutoResetEvent 은 를 사용하여 만든 EventResetMode.AutoResetEventWaitHandle 기능적으로 동일합니다.

참고

클래스와 AutoResetEvent 달리 클래스는 EventWaitHandle 명명된 시스템 동기화 이벤트에 대한 액세스를 제공합니다.

중요

이 형식이 구현 하는 IDisposable 인터페이스입니다. 형식을 사용 하 여 마쳤으면 직접 또는 간접적으로의 삭제 해야 있습니다. 직접 형식의 dispose 호출 해당 Dispose 의 메서드를 try/catch 블록입니다. 삭제 하지 직접, 언어 구문 같은 사용 using (C#에서) 또는 Using (Visual Basic에서는). 자세한 내용은 인터페이스 페이지의 "IDisposable을 구현하는 개체 사용" 섹션을 IDisposable 참조하세요.

생성자

AutoResetEvent(Boolean)

초기 상태를 신호 받음으로 설정할지 여부를 나타내는 부울 값을 사용하여 AutoResetEvent 클래스의 새 인스턴스를 초기화합니다.

필드

WaitTimeout

대기 핸들이 신호를 받기 전에 WaitAny(WaitHandle[], Int32, Boolean) 작업이 제한 시간을 초과했음을 나타냅니다. 이 필드는 상수입니다.

(다음에서 상속됨 WaitHandle)

속성

Handle
사용되지 않음.
사용되지 않음.

네이티브 운영 체제 핸들을 가져오거나 설정합니다.

(다음에서 상속됨 WaitHandle)
SafeWaitHandle

네이티브 운영 체제 핸들을 가져오거나 설정합니다.

(다음에서 상속됨 WaitHandle)

메서드

Close()

현재 WaitHandle에서 보유한 모든 리소스를 해제합니다.

(다음에서 상속됨 WaitHandle)
CreateObjRef(Type)

원격 개체와 통신하는 데 사용되는 프록시 생성에 필요한 모든 관련 정보가 들어 있는 개체를 만듭니다.

(다음에서 상속됨 MarshalByRefObject)
Dispose()

WaitHandle 클래스의 현재 인스턴스에서 사용하는 모든 리소스를 해제합니다.

(다음에서 상속됨 WaitHandle)
Dispose(Boolean)

파생 클래스에서 재정의된 경우 WaitHandle에서 사용하는 관리되지 않는 리소스를 해제하고 필요에 따라 관리되는 리소스를 해제합니다.

(다음에서 상속됨 WaitHandle)
Equals(Object)

지정된 개체가 현재 개체와 같은지 확인합니다.

(다음에서 상속됨 Object)
GetAccessControl()

현재 EventWaitHandleSecurity 개체로 표시되는 명명된 시스템 이벤트에 대한 액세스 제어 보안을 나타내는 EventWaitHandle 개체를 가져옵니다.

(다음에서 상속됨 EventWaitHandle)
GetHashCode()

기본 해시 함수로 작동합니다.

(다음에서 상속됨 Object)
GetLifetimeService()
사용되지 않음.

이 인스턴스의 수명 정책을 제어하는 현재의 수명 서비스 개체를 검색합니다.

(다음에서 상속됨 MarshalByRefObject)
GetType()

현재 인스턴스의 Type을 가져옵니다.

(다음에서 상속됨 Object)
InitializeLifetimeService()
사용되지 않음.

이 인스턴스의 수명 정책을 제어하는 수명 서비스 개체를 가져옵니다.

(다음에서 상속됨 MarshalByRefObject)
MemberwiseClone()

현재 Object의 단순 복사본을 만듭니다.

(다음에서 상속됨 Object)
MemberwiseClone(Boolean)

현재 MarshalByRefObject 개체의 단순 복사본을 만듭니다.

(다음에서 상속됨 MarshalByRefObject)
Reset()

스레드가 차단되도록 이벤트 상태를 신호 없음으로 설정합니다.

Reset()

스레드가 차단되도록 이벤트 상태를 신호 없음으로 설정합니다.

(다음에서 상속됨 EventWaitHandle)
Set()

대기 중인 스레드 중 하나만 진행되도록 이벤트 상태를 신호 받음으로 설정합니다.

Set()

하나 이상의 대기 중인 스레드가 계속 진행되도록 이벤트 상태를 신호 받음으로 설정합니다.

(다음에서 상속됨 EventWaitHandle)
SetAccessControl(EventWaitHandleSecurity)

명명된 시스템 이벤트에 대한 액세스 제어 보안을 설정합니다.

(다음에서 상속됨 EventWaitHandle)
ToString()

현재 개체를 나타내는 문자열을 반환합니다.

(다음에서 상속됨 Object)
WaitOne()

현재 WaitHandle이(가) 신호를 받을 때까지 현재 스레드를 차단합니다.

(다음에서 상속됨 WaitHandle)
WaitOne(Int32)

부호 있는 32비트 정수로 시간 간격(밀리초)을 지정하여 현재 WaitHandle이 신호를 받을 때까지 현재 스레드를 차단합니다.

(다음에서 상속됨 WaitHandle)
WaitOne(Int32, Boolean)

부호 있는 32비트 정수로 시간 간격을 지정하고 대기 전에 동기화 도메인을 끝낼지 여부를 지정하여 현재 WaitHandle이 신호를 받을 때까지 현재 스레드를 차단합니다.

(다음에서 상속됨 WaitHandle)
WaitOne(TimeSpan)

TimeSpan로 시간 간격을 지정하여 현재 인스턴스가 신호를 받을 때까지 현재 스레드를 차단합니다.

(다음에서 상속됨 WaitHandle)
WaitOne(TimeSpan, Boolean)

TimeSpan로 시간 간격을 지정하고 대기 전에 동기화 도메인을 끝낼지 여부를 지정하여 현재 인스턴스가 신호를 받을 때까지 현재 스레드를 차단합니다.

(다음에서 상속됨 WaitHandle)

명시적 인터페이스 구현

IDisposable.Dispose()

이 API는 제품 인프라를 지원하며 코드에서 직접 사용되지 않습니다.

WaitHandle에서 사용하는 모든 리소스를 해제합니다.

(다음에서 상속됨 WaitHandle)

확장 메서드

GetAccessControl(EventWaitHandle)

지정된 handle에 대한 보안 설명자를 반환합니다.

SetAccessControl(EventWaitHandle, EventWaitHandleSecurity)

지정된 이벤트 대기 핸들에 대한 보안 설명자를 설정합니다.

GetSafeWaitHandle(WaitHandle)

네이티브 운영 체제 대기 핸들에 대한 안전한 핸들을 가져옵니다.

SetSafeWaitHandle(WaitHandle, SafeWaitHandle)

네이티브 운영 체제 대기 핸들에 대한 안전한 핸들을 설정합니다.

적용 대상

스레드 보안

이 클래스는 스레드로부터 안전합니다.

추가 정보