EventWaitHandle 클래스
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
스레드 동기화 이벤트를 나타냅니다.
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
- 상속
- 상속
- 파생
- 특성
예제
다음 코드 예제에서는 메서드 오버로드를 사용하여 SignalAndWait(WaitHandle, WaitHandle) 주 스레드가 차단된 스레드에 신호를 보낼 수 있도록 한 다음 스레드가 작업을 완료할 때까지 기다립니다.
이 예제에서는 5개의 스레드를 시작하고 플래그를 사용하여 만든 EventResetMode.AutoReset 스레드를 EventWaitHandle 차단한 다음 사용자가 Enter 키를 누를 때마다 하나의 스레드를 해제할 수 있습니다. 그런 다음 이 예제에서는 다른 5개의 스레드를 큐에 대기하고 플래그와 함께 EventResetMode.ManualReset 만든 스레드를 EventWaitHandle 사용하여 모두 해제합니다.
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 를 해제할 때까지 하나 이상의 스레드가 차단됩니다. 스레드는 EventWaitHandle 신호를 받은 다음 static(Visual Basic Shared) WaitHandle.SignalAndWait 메서드를 호출하여 차단할 수 있습니다.
메모
클래스는 EventWaitHandle 명명된 시스템 동기화 이벤트에 대한 액세스를 제공합니다.
신호를 받은 동작 EventWaitHandle 은 다시 설정 모드에 따라 달라집니다. EventWaitHandle 플래그를 사용하여 EventResetMode.AutoReset 만든 것은 단일 대기 스레드를 해제한 후 신호를 받으면 자동으로 다시 설정됩니다. EventWaitHandle는 EventResetMode.ManualReset 플래그로 생성될 경우, Reset 메서드가 호출될 때까지 신호 상태를 유지합니다.
자동 재설정 이벤트는 리소스에 대한 단독 액세스를 제공합니다. 대기 중인 스레드가 없을 때 자동 재설정 이벤트가 신호를 받으면 스레드가 대기하려고 할 때까지 신호를 유지합니다. 이 이벤트는 스레드를 해제하고 즉시 다시 설정하여 후속 스레드를 차단합니다.
수동 재설정 이벤트는 게이트와 같습니다. 이벤트가 신호를 받지 않으면 이벤트가 대기하는 스레드가 차단됩니다. 이벤트가 신호를 받으면 모든 대기 스레드가 해제되고 메서드가 호출될 때까지 Reset 이벤트가 신호로 유지됩니다(즉, 후속 대기가 차단되지 않음). 수동 다시 설정 이벤트는 다른 스레드를 계속 진행하기 전에 한 스레드가 작업을 완료해야 하는 경우에 유용합니다.
EventWaitHandle 개체는 static(Visual Basic Shared) WaitHandle.WaitAll 및 WaitHandle.WaitAny 메서드와 함께 사용할 수 있습니다.
자세한 내용은 동기화 기본 형식 개요 문서의 스레드 상호 작용 또는 신호 섹션을 참조하세요.
주의
기본적으로 명명된 이벤트는 해당 이벤트를 만든 사용자로 제한되지 않습니다. 다른 사용자는 이벤트를 부적절하게 설정하거나 다시 설정하여 이벤트를 방해하는 등 이벤트를 열고 사용할 수 있습니다. 특정 사용자에 대한 액세스를 제한하려면 생성자 오버로드를 사용하거나 EventWaitHandleAcl 명명된 EventWaitHandleSecurity 이벤트를 만들 때 전달합니다. 코드를 실행하는 신뢰할 수 없는 사용자가 있을 수 있는 시스템에 대한 액세스 제한 없이 명명된 이벤트를 사용하지 마세요.
생성자
| Name | Description |
|---|---|
| EventWaitHandle(Boolean, EventResetMode, String, Boolean, EventWaitHandleSecurity) |
이 호출의 EventWaitHandle 결과로 생성된 경우 대기 핸들이 처음에 신호를 받을지 여부, 자동으로 또는 수동으로 다시 설정되는지 여부, 시스템 동기화 이벤트의 이름, 호출 후 값이 명명된 시스템 이벤트가 생성되었는지 여부를 나타내는 부울 변수, 명명된 이벤트 생성 시 액세스 제어 보안을 적용할 것인지를 지정하여 클래스의 새 인스턴스를 초기화합니다. |
| EventWaitHandle(Boolean, EventResetMode, String, Boolean) |
이 호출의 EventWaitHandle 결과로 생성된 경우 대기 핸들이 처음 신호를 받는지 여부, 자동으로 또는 수동으로 다시 설정되는지 여부, 시스템 동기화 이벤트의 이름 및 호출 후 값이 명명된 시스템 이벤트가 생성되었는지 여부를 나타내는 부울 변수를 지정하여 클래스의 새 인스턴스를 초기화합니다. |
| EventWaitHandle(Boolean, EventResetMode, String, NamedWaitHandleOptions, Boolean) |
이 호출의 EventWaitHandle 결과로 생성된 경우 대기 핸들이 처음에 신호를 받는지 여부, 자동으로 또는 수동으로 다시 설정되는지 여부, 시스템 동기화 이벤트의 이름, 사용자 범위 및 세션 범위 액세스를 설정하는 옵션, 호출 후 값이 명명된 시스템 이벤트가 생성되었는지 여부를 나타내는 부울 변수를 지정하여 클래스의 새 인스턴스를 초기화합니다. |
| EventWaitHandle(Boolean, EventResetMode, String, NamedWaitHandleOptions) |
이 호출의 EventWaitHandle 결과로 만들어진 경우 대기 핸들이 처음에 신호를 보낼지 여부, 자동으로 또는 수동으로 다시 설정되는지 여부, 시스템 동기화 이벤트의 이름 및 사용자 범위 및 세션 범위 액세스를 설정하는 옵션을 지정하여 클래스의 새 인스턴스를 초기화합니다. |
| EventWaitHandle(Boolean, EventResetMode, String) |
이 호출의 EventWaitHandle 결과로 생성된 경우 대기 핸들이 처음에 신호를 보낼지 여부, 자동으로 또는 수동으로 다시 설정되는지 여부 및 시스템 동기화 이벤트의 이름을 지정하여 클래스의 새 인스턴스를 초기화합니다. |
| EventWaitHandle(Boolean, EventResetMode) |
대기 핸들이 처음에 신호를 보낼지 여부와 자동으로 또는 수동으로 다시 설정되는지 여부를 지정하여 클래스의 EventWaitHandle 새 인스턴스를 초기화합니다. |
필드
| Name | Description |
|---|---|
| WaitTimeout |
대기 핸들이 WaitAny(WaitHandle[], Int32, Boolean) 신호를 받기 전에 작업 시간이 초과되었음을 나타냅니다. 이 필드는 상수입니다. (다음에서 상속됨 WaitHandle) |
속성
| Name | Description |
|---|---|
| Handle |
사용되지 않음.
사용되지 않음.
네이티브 운영 체제 핸들을 가져오거나 설정합니다. (다음에서 상속됨 WaitHandle) |
| SafeWaitHandle |
네이티브 운영 체제 핸들을 가져오거나 설정합니다. (다음에서 상속됨 WaitHandle) |
메서드
| Name | Description |
|---|---|
| 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, EventWaitHandleRights) |
지정된 명명된 동기화 이벤트가 이미 있는 경우 원하는 보안 액세스 권한으로 엽니다. |
| OpenExisting(String, NamedWaitHandleOptions) |
지정된 명명된 동기화 이벤트가 이미 있는 경우 엽니다. 옵션이 현재 사용자로만 설정된 경우 호출하는 사용자에 대해 개체의 액세스 제어가 확인됩니다. |
| OpenExisting(String) |
지정된 명명된 동기화 이벤트가 이미 있는 경우 엽니다. |
| Reset() |
이벤트의 상태를 서명되지 않은 상태로 설정하여 스레드가 차단되도록 합니다. |
| Set() |
이벤트 상태를 신호로 설정하여 하나 이상의 대기 스레드가 계속 진행되도록 합니다. |
| SetAccessControl(EventWaitHandleSecurity) |
명명된 시스템 이벤트에 대한 액세스 제어 보안을 설정합니다. |
| ToString() |
현재 개체를 나타내는 문자열을 반환합니다. (다음에서 상속됨 Object) |
| TryOpenExisting(String, EventWaitHandle) |
지정된 명명된 동기화 이벤트가 이미 있는 경우 열고 작업이 성공했는지 여부를 나타내는 값을 반환합니다. |
| TryOpenExisting(String, EventWaitHandleRights, EventWaitHandle) |
지정된 명명된 동기화 이벤트가 이미 있는 경우 원하는 보안 액세스 권한으로 열고 작업이 성공했는지 여부를 나타내는 값을 반환합니다. |
| TryOpenExisting(String, NamedWaitHandleOptions, EventWaitHandle) |
지정된 명명된 동기화 이벤트가 이미 있는 경우 열고 작업이 성공했는지 여부를 나타내는 값을 반환합니다. 옵션이 현재 사용자로만 설정된 경우 호출하는 사용자에 대해 개체의 액세스 제어가 확인됩니다. |
| WaitOne() |
현재 스레드가 신호를 받을 때까지 현재 WaitHandle 스레드를 차단합니다. (다음에서 상속됨 WaitHandle) |
| WaitOne(Int32, Boolean) |
32비트 부호 있는 정수를 사용하여 시간 간격을 지정하고 대기 전에 동기화 도메인을 종료할지 여부를 지정하여 현재 스레드가 신호를 받을 때까지 현재 WaitHandle 스레드를 차단합니다. (다음에서 상속됨 WaitHandle) |
| WaitOne(Int32) |
32비트 부호 있는 정수를 사용하여 시간 간격을 밀리초 단위로 지정하여 현재 스레드가 신호를 받을 때까지 현재 WaitHandle 스레드를 차단합니다. (다음에서 상속됨 WaitHandle) |
| WaitOne(TimeSpan, Boolean) |
시간 간격을 지정하고 대기 전에 동기화 도메인을 종료할지 여부를 지정하여 현재 인스턴스가 신호를 TimeSpan 받을 때까지 현재 스레드를 차단합니다. (다음에서 상속됨 WaitHandle) |
| WaitOne(TimeSpan) |
시간 간격을 지정하여 현재 인스턴스가 신호를 받을 때까지 현재 스레드를 TimeSpan 차단합니다. (다음에서 상속됨 WaitHandle) |
명시적 인터페이스 구현
| Name | Description |
|---|---|
| IDisposable.Dispose() |
이 API는 제품 인프라를 지원하며 코드에서 직접 사용되지 않습니다. 에서 사용하는 모든 리소스를 WaitHandle해제합니다. (다음에서 상속됨 WaitHandle) |
확장명 메서드
| Name | Description |
|---|---|
| GetAccessControl(EventWaitHandle) |
지정된 |
| GetSafeWaitHandle(WaitHandle) |
네이티브 운영 체제 대기 핸들에 대한 안전 핸들을 가져옵니다. |
| SetAccessControl(EventWaitHandle, EventWaitHandleSecurity) |
지정된 이벤트 대기 핸들에 대한 보안 설명자를 설정합니다. |
| SetSafeWaitHandle(WaitHandle, SafeWaitHandle) |
네이티브 운영 체제 대기 핸들에 대한 안전 핸들을 설정합니다. |
적용 대상
스레드 보안
이 형식은 스레드로부터 안전합니다.