EventWaitHandle 클래스

정의

스레드 동기화 이벤트를 나타냅니다.

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

예제

다음 코드 예제에서는 메서드 오버로드를 사용하여 SignalAndWait(WaitHandle, WaitHandle) 기본 스레드가 차단된 스레드에 신호를 보냅니다. 그런 다음 스레드가 작업을 완료할 때까지 기다립니다.

이 예제에서는 5개의 스레드를 시작하고 플래그를 사용하여 만든 EventResetMode.AutoReset 스레드에서 EventWaitHandle 차단한 다음 사용자가 Enter 키를 누를 때마다 스레드 하나를 해제할 수 있습니다. 그런 다음, 이 예제에서는 다른 5개의 스레드를 큐에 대기시키고 플래그를 사용하여 만든 EventResetMode.ManualResetEventWaitHandle 사용하여 모두 해제합니다.

using namespace System;
using namespace System::Threading;

public ref class Example
{
private:
   // The EventWaitHandle used to demonstrate the difference
   // between AutoReset and ManualReset synchronization events.
   //
   static EventWaitHandle^ ewh;

   // A counter to make sure all threads are started and
   // blocked before any are released. A Long is used to show
   // the use of the 64-bit Interlocked methods.
   //
   static __int64 threadCount = 0;

   // An AutoReset event that allows the main thread to block
   // until an exiting thread has decremented the count.
   //
   static EventWaitHandle^ clearCount =
      gcnew EventWaitHandle( false,EventResetMode::AutoReset );

public:
   [MTAThread]
   static void main()
   {
      // Create an AutoReset EventWaitHandle.
      //
      ewh = gcnew EventWaitHandle( false,EventResetMode::AutoReset );
      
      // Create and start five numbered threads. Use the
      // ParameterizedThreadStart delegate, so the thread
      // number can be passed as an argument to the Start
      // method.
      for ( int i = 0; i <= 4; i++ )
      {
         Thread^ t = gcnew Thread(
            gcnew ParameterizedThreadStart( ThreadProc ) );
         t->Start( i );
      }
      
      // Wait until all the threads have started and blocked.
      // When multiple threads use a 64-bit value on a 32-bit
      // system, you must access the value through the
      // Interlocked class to guarantee thread safety.
      //
      while ( Interlocked::Read( threadCount ) < 5 )
      {
         Thread::Sleep( 500 );
      }

      // Release one thread each time the user presses ENTER,
      // until all threads have been released.
      //
      while ( Interlocked::Read( threadCount ) > 0 )
      {
         Console::WriteLine( L"Press ENTER to release a waiting thread." );
         Console::ReadLine();
         
         // SignalAndWait signals the EventWaitHandle, which
         // releases exactly one thread before resetting,
         // because it was created with AutoReset mode.
         // SignalAndWait then blocks on clearCount, to
         // allow the signaled thread to decrement the count
         // before looping again.
         //
         WaitHandle::SignalAndWait( ewh, clearCount );
      }
      Console::WriteLine();
      
      // Create a ManualReset EventWaitHandle.
      //
      ewh = gcnew EventWaitHandle( false,EventResetMode::ManualReset );
      
      // Create and start five more numbered threads.
      //
      for ( int i = 0; i <= 4; i++ )
      {
         Thread^ t = gcnew Thread(
            gcnew ParameterizedThreadStart( ThreadProc ) );
         t->Start( i );
      }
      
      // Wait until all the threads have started and blocked.
      //
      while ( Interlocked::Read( threadCount ) < 5 )
      {
         Thread::Sleep( 500 );
      }

      // Because the EventWaitHandle was created with
      // ManualReset mode, signaling it releases all the
      // waiting threads.
      //
      Console::WriteLine( L"Press ENTER to release the waiting threads." );
      Console::ReadLine();
      ewh->Set();

   }

   static void ThreadProc( Object^ data )
   {
      int index = static_cast<Int32>(data);

      Console::WriteLine( L"Thread {0} blocks.", data );
      // Increment the count of blocked threads.
      Interlocked::Increment( threadCount );
      
      // Wait on the EventWaitHandle.
      ewh->WaitOne();

      Console::WriteLine( L"Thread {0} exits.", data );
      // Decrement the count of blocked threads.
      Interlocked::Decrement( threadCount );
      
      // After signaling ewh, the main thread blocks on
      // clearCount until the signaled thread has
      // decremented the count. Signal it now.
      //
      clearCount->Set();
   }
};
using System;
using System.Threading;

public class Example
{
    // The EventWaitHandle used to demonstrate the difference
    // between AutoReset and ManualReset synchronization events.
    //
    private static EventWaitHandle ewh;

    // A counter to make sure all threads are started and
    // blocked before any are released. A Long is used to show
    // the use of the 64-bit Interlocked methods.
    //
    private static long threadCount = 0;

    // An AutoReset event that allows the main thread to block
    // until an exiting thread has decremented the count.
    //
    private static EventWaitHandle clearCount = 
        new EventWaitHandle(false, EventResetMode.AutoReset);

    [MTAThread]
    public static void Main()
    {
        // Create an AutoReset EventWaitHandle.
        //
        ewh = new EventWaitHandle(false, EventResetMode.AutoReset);

        // Create and start five numbered threads. Use the
        // ParameterizedThreadStart delegate, so the thread
        // number can be passed as an argument to the Start 
        // method.
        for (int i = 0; i <= 4; i++)
        {
            Thread t = new Thread(
                new ParameterizedThreadStart(ThreadProc)
            );
            t.Start(i);
        }

        // Wait until all the threads have started and blocked.
        // When multiple threads use a 64-bit value on a 32-bit
        // system, you must access the value through the
        // Interlocked class to guarantee thread safety.
        //
        while (Interlocked.Read(ref threadCount) < 5)
        {
            Thread.Sleep(500);
        }

        // Release one thread each time the user presses ENTER,
        // until all threads have been released.
        //
        while (Interlocked.Read(ref threadCount) > 0)
        {
            Console.WriteLine("Press ENTER to release a waiting thread.");
            Console.ReadLine();

            // SignalAndWait signals the EventWaitHandle, which
            // releases exactly one thread before resetting, 
            // because it was created with AutoReset mode. 
            // SignalAndWait then blocks on clearCount, to 
            // allow the signaled thread to decrement the count
            // before looping again.
            //
            WaitHandle.SignalAndWait(ewh, clearCount);
        }
        Console.WriteLine();

        // Create a ManualReset EventWaitHandle.
        //
        ewh = new EventWaitHandle(false, EventResetMode.ManualReset);

        // Create and start five more numbered threads.
        //
        for(int i=0; i<=4; i++)
        {
            Thread t = new Thread(
                new ParameterizedThreadStart(ThreadProc)
            );
            t.Start(i);
        }

        // Wait until all the threads have started and blocked.
        //
        while (Interlocked.Read(ref threadCount) < 5)
        {
            Thread.Sleep(500);
        }

        // Because the EventWaitHandle was created with
        // ManualReset mode, signaling it releases all the
        // waiting threads.
        //
        Console.WriteLine("Press ENTER to release the waiting threads.");
        Console.ReadLine();
        ewh.Set();
    }

    public static void ThreadProc(object data)
    {
        int index = (int) data;

        Console.WriteLine("Thread {0} blocks.", data);
        // Increment the count of blocked threads.
        Interlocked.Increment(ref threadCount);

        // Wait on the EventWaitHandle.
        ewh.WaitOne();

        Console.WriteLine("Thread {0} exits.", data);
        // Decrement the count of blocked threads.
        Interlocked.Decrement(ref threadCount);

        // After signaling ewh, the main thread blocks on
        // clearCount until the signaled thread has 
        // decremented the count. Signal it now.
        //
        clearCount.Set();
    }
}
Imports System.Threading

Public Class Example

    ' The EventWaitHandle used to demonstrate the difference
    ' between AutoReset and ManualReset synchronization events.
    '
    Private Shared ewh As EventWaitHandle

    ' A counter to make sure all threads are started and
    ' blocked before any are released. A Long is used to show
    ' the use of the 64-bit Interlocked methods.
    '
    Private Shared threadCount As Long = 0

    ' An AutoReset event that allows the main thread to block
    ' until an exiting thread has decremented the count.
    '
    Private Shared clearCount As New EventWaitHandle(False, _
        EventResetMode.AutoReset)

    <MTAThread> _
    Public Shared Sub Main()

        ' Create an AutoReset EventWaitHandle.
        '
        ewh = New EventWaitHandle(False, EventResetMode.AutoReset)

        ' Create and start five numbered threads. Use the
        ' ParameterizedThreadStart delegate, so the thread
        ' number can be passed as an argument to the Start 
        ' method.
        For i As Integer = 0 To 4
            Dim t As New Thread(AddressOf ThreadProc)
            t.Start(i)
        Next i

        ' Wait until all the threads have started and blocked.
        ' When multiple threads use a 64-bit value on a 32-bit
        ' system, you must access the value through the
        ' Interlocked class to guarantee thread safety.
        '
        While Interlocked.Read(threadCount) < 5
            Thread.Sleep(500)
        End While

        ' Release one thread each time the user presses ENTER,
        ' until all threads have been released.
        '
        While Interlocked.Read(threadCount) > 0
            Console.WriteLine("Press ENTER to release a waiting thread.")
            Console.ReadLine()

            ' SignalAndWait signals the EventWaitHandle, which
            ' releases exactly one thread before resetting, 
            ' because it was created with AutoReset mode. 
            ' SignalAndWait then blocks on clearCount, to 
            ' allow the signaled thread to decrement the count
            ' before looping again.
            '
            WaitHandle.SignalAndWait(ewh, clearCount)
        End While
        Console.WriteLine()

        ' Create a ManualReset EventWaitHandle.
        '
        ewh = New EventWaitHandle(False, EventResetMode.ManualReset)

        ' Create and start five more numbered threads.
        '
        For i As Integer = 0 To 4
            Dim t As New Thread(AddressOf ThreadProc)
            t.Start(i)
        Next i

        ' Wait until all the threads have started and blocked.
        '
        While Interlocked.Read(threadCount) < 5
            Thread.Sleep(500)
        End While

        ' Because the EventWaitHandle was created with
        ' ManualReset mode, signaling it releases all the
        ' waiting threads.
        '
        Console.WriteLine("Press ENTER to release the waiting threads.")
        Console.ReadLine()
        ewh.Set()
        
    End Sub

    Public Shared Sub ThreadProc(ByVal data As Object)
        Dim index As Integer = CInt(data)

        Console.WriteLine("Thread {0} blocks.", data)
        ' Increment the count of blocked threads.
        Interlocked.Increment(threadCount)

        ' Wait on the EventWaitHandle.
        ewh.WaitOne()

        Console.WriteLine("Thread {0} exits.", data)
        ' Decrement the count of blocked threads.
        Interlocked.Decrement(threadCount)

        ' After signaling ewh, the main thread blocks on
        ' clearCount until the signaled thread has 
        ' decremented the count. Signal it now.
        '
        clearCount.Set()
    End Sub
End Class

설명

EventWaitHandle 클래스를 사용하면 스레드가 신호를 통해 서로 통신할 수 있습니다. 일반적으로 차단 해제된 스레드가 메서드를 호출 Set 하여 하나 이상의 차단 EventWaitHandle 된 스레드를 해제할 때까지 하나 이상의 스레드가 에서 차단됩니다. 스레드는 (SharedVisual Basic의 경우) WaitHandle.SignalAndWait 메서드를 EventWaitHandle 호출 static 하여 신호를 받은 다음 이를 차단할 수 있습니다.

참고

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

신호를 받은 의 EventWaitHandle 동작은 다시 설정 모드에 따라 달라집니다. 플래그를 사용하여 EventResetMode.AutoReset 만든 는 EventWaitHandle 단일 대기 스레드를 해제한 후 신호를 받으면 자동으로 다시 설정됩니다. EventResetMode.ManualReset 플래그를 사용하여 만들어진 EventWaitHandleReset 메서드가 호출될 때까지 신호 알림 상태를 유지합니다.

자동 재설정 이벤트는 리소스에 대한 단독 액세스를 제공합니다. 대기 중인 스레드가 없을 때 신호를 받은 자동 재설정 이벤트는 스레드가 이 이벤트에서 대기를 시도할 때까지 신호를 받은 것으로 유지됩니다. 이벤트는 스레드를 해제하고 즉시 다시 설정되어 후속 스레드를 차단합니다.

수동 재설정 이벤트는 게이트와 같습니다. 이벤트가 신호를 받지 않으면 이벤트를 기다리는 스레드가 차단됩니다. 이벤트가 신호를 받으면 모든 대기 스레드가 해제되고 메서드가 호출될 때까지 Reset 이벤트가 신호로 유지됩니다(즉, 후속 대기가 차단되지 않음). 수동 다시 설정 이벤트는 다른 스레드가 계속 진행되기 전에 한 스레드가 작업을 완료해야 하는 경우에 유용합니다.

EventWaitHandle개체는 (SharedVisual Basic의 static경우) WaitHandle.WaitAll 및 메서드와 WaitHandle.WaitAny 함께 사용할 수 있습니다.

자세한 내용은 동기화 기본 형식 개요 문서의 스레드 상호 작용 또는 신호 섹션을 참조하세요.

주의

기본적으로 명명된 이벤트는 이벤트를 만든 사용자로 제한되지 않습니다. 다른 사용자는 이벤트를 설정하거나 부적절하게 다시 설정하여 이벤트를 방해하는 등 이벤트를 열고 사용할 수 있습니다. 특정 사용자에 대한 액세스를 제한하려면 생성자 오버로드를 사용하거나 EventWaitHandleAcl 명명된 이벤트를 만들 때 를 전달할 EventWaitHandleSecurity 수 있습니다. 신뢰할 수 없는 사용자가 코드를 실행 중일 수 있는 시스템에 대한 액세스 제한 없이 명명된 이벤트를 사용하지 마세요.

생성자

EventWaitHandle(Boolean, EventResetMode)

대기 핸들의 초기 상태를 신호 받음으로 설정할지 여부와 대기 핸들을 자동으로 다시 설정할지 수동으로 다시 설정할지 여부를 지정하여 EventWaitHandle 클래스의 새 인스턴스를 초기화합니다.

EventWaitHandle(Boolean, EventResetMode, String)

이 호출의 결과로 만들어진 대기 핸들의 초기 상태를 신호 받음으로 설정할지 여부, 대기 핸들을 자동으로 다시 설정할지 수동으로 다시 설정할지 여부 및 시스템 동기화 이벤트의 이름을 지정하여 EventWaitHandle 클래스의 새 인스턴스를 초기화합니다.

EventWaitHandle(Boolean, EventResetMode, String, Boolean)

이 호출의 결과로 대기 핸들이 초기에 신호를 받는지 여부, 자동으로 재설정되는지 또는 수동으로 재설정되는지, 시스템 동기화 이벤트의 이름, 호출 후에 해당 값이 명명된 시스템 이벤트가 생성되었는지 여부를 나타내는 부울 변수를 지정하여 EventWaitHandle 클래스의 새 인스턴스를 초기화합니다.

EventWaitHandle(Boolean, EventResetMode, String, Boolean, EventWaitHandleSecurity)

이 호출의 결과로 대기 핸들이 초기에 신호를 받는지 여부, 자동으로 재설정되는지 또는 수동으로 재설정되는지, 시스템 동기화 이벤트의 이름, 호출 후에 해당 값이 명명된 시스템 이벤트가 생성되었는지 여부를 나타내는 부울 변수, 생성되는 경우 명명된 이벤트에 적용될 액세스 제어 보안을 지정하여 EventWaitHandle 클래스의 새 인스턴스를 초기화합니다.

필드

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 개체를 가져옵니다.

GetHashCode()

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

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

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

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

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

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

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

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

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

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

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

(다음에서 상속됨 MarshalByRefObject)
OpenExisting(String)

이미 있는 경우 지정한 명명된 동기화 이벤트를 엽니다.

OpenExisting(String, EventWaitHandleRights)

이미 있는 경우 지정한 명명된 동기화 이벤트를 원하는 보안 액세스로 엽니다.

Reset()

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

Set()

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

SetAccessControl(EventWaitHandleSecurity)

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

ToString()

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

(다음에서 상속됨 Object)
TryOpenExisting(String, EventWaitHandle)

지정한 명명된 동기화 이벤트(이미 존재하는 경우)를 열고 작업이 성공했는지를 나타내는 값을 반환합니다.

TryOpenExisting(String, EventWaitHandleRights, EventWaitHandle)

지정한 명명된 동기화 이벤트(이미 존재하는 경우)를 원하는 보안 액세스로 열고 작업이 성공적으로 수행되었는지 여부를 나타내는 값을 반환합니다.

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)

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

적용 대상

스레드 보안

이 형식은 스레드로부터 안전합니다.

추가 정보