ManualResetEvent 클래스
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
신호를 받을 때 수동으로 다시 설정해야 하는 스레드 동기화 이벤트를 나타냅니다. 이 클래스는 상속할 수 없습니다.
public ref class ManualResetEvent sealed : System::Threading::EventWaitHandle
public ref class ManualResetEvent sealed : System::Threading::WaitHandle
public sealed class ManualResetEvent : System.Threading.EventWaitHandle
public sealed class ManualResetEvent : System.Threading.WaitHandle
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class ManualResetEvent : System.Threading.EventWaitHandle
type ManualResetEvent = class
inherit EventWaitHandle
type ManualResetEvent = class
inherit WaitHandle
[<System.Runtime.InteropServices.ComVisible(true)>]
type ManualResetEvent = class
inherit EventWaitHandle
Public NotInheritable Class ManualResetEvent
Inherits EventWaitHandle
Public NotInheritable Class ManualResetEvent
Inherits WaitHandle
- 상속
- 상속
- 상속
- 특성
예제
다음 예제에서는 작동 방식을 ManualResetEvent 보여 줍니다. 이 예제는 서명되지 않은 상태(즉, ManualResetEvent 생성자에 전달됨)로 시작 false 합니다. 이 예제에서는 메서드를 호출하여 각 스레드를 ManualResetEvent 차단하는 세 개의 스레드를 WaitOne 만듭니다. 사용자가 Enter 키를 누르면 이 예제에서는 세 스레드를 Set 모두 해제하는 메서드를 호출합니다. 스레드를 한 번에 하나씩 해제하고 각 릴리스 후에 자동으로 다시 설정되는 클래스의 AutoResetEvent 동작과 대조합니다.
Enter 키를 다시 누르면 메서드가 ManualResetEvent 호출될 때까지 신호 상태가 유지됨을 Reset 보여 줍니다. 이 예제에서는 두 개의 스레드를 더 시작합니다. 이러한 스레드는 메서드를 호출 WaitOne 할 때 차단되지 않고 완료될 때 실행됩니다.
Enter 키를 다시 누르면 예제에서 메서드를 호출 Reset 하고 스레드를 하나 더 시작하면 호출WaitOne할 때 차단됩니다. Enter 키를 눌러 마지막 스레드를 해제하기 위해 마지막으로 한 번 호출 Set 하면 프로그램이 종료됩니다.
using System;
using System.Threading;
public class Example
{
// mre is used to block and release threads manually. It is
// created in the unsignaled state.
private static ManualResetEvent mre = new ManualResetEvent(false);
static void Main()
{
Console.WriteLine("\nStart 3 named threads that block on a ManualResetEvent:\n");
for(int i = 0; i <= 2; i++)
{
Thread t = new Thread(ThreadProc);
t.Name = "Thread_" + i;
t.Start();
}
Thread.Sleep(500);
Console.WriteLine("\nWhen all three threads have started, press Enter to call Set()" +
"\nto release all the threads.\n");
Console.ReadLine();
mre.Set();
Thread.Sleep(500);
Console.WriteLine("\nWhen a ManualResetEvent is signaled, threads that call WaitOne()" +
"\ndo not block. Press Enter to show this.\n");
Console.ReadLine();
for(int i = 3; i <= 4; i++)
{
Thread t = new Thread(ThreadProc);
t.Name = "Thread_" + i;
t.Start();
}
Thread.Sleep(500);
Console.WriteLine("\nPress Enter to call Reset(), so that threads once again block" +
"\nwhen they call WaitOne().\n");
Console.ReadLine();
mre.Reset();
// Start a thread that waits on the ManualResetEvent.
Thread t5 = new Thread(ThreadProc);
t5.Name = "Thread_5";
t5.Start();
Thread.Sleep(500);
Console.WriteLine("\nPress Enter to call Set() and conclude the demo.");
Console.ReadLine();
mre.Set();
// If you run this example in Visual Studio, uncomment the following line:
//Console.ReadLine();
}
private static void ThreadProc()
{
string name = Thread.CurrentThread.Name;
Console.WriteLine(name + " starts and calls mre.WaitOne()");
mre.WaitOne();
Console.WriteLine(name + " ends.");
}
}
/* This example produces output similar to the following:
Start 3 named threads that block on a ManualResetEvent:
Thread_0 starts and calls mre.WaitOne()
Thread_1 starts and calls mre.WaitOne()
Thread_2 starts and calls mre.WaitOne()
When all three threads have started, press Enter to call Set()
to release all the threads.
Thread_2 ends.
Thread_0 ends.
Thread_1 ends.
When a ManualResetEvent is signaled, threads that call WaitOne()
do not block. Press Enter to show this.
Thread_3 starts and calls mre.WaitOne()
Thread_3 ends.
Thread_4 starts and calls mre.WaitOne()
Thread_4 ends.
Press Enter to call Reset(), so that threads once again block
when they call WaitOne().
Thread_5 starts and calls mre.WaitOne()
Press Enter to call Set() and conclude the demo.
Thread_5 ends.
*/
Imports System.Threading
Public Class Example
' mre is used to block and release threads manually. It is
' created in the unsignaled state.
Private Shared mre As New ManualResetEvent(False)
<MTAThreadAttribute> _
Shared Sub Main()
Console.WriteLine(vbLf & _
"Start 3 named threads that block on a ManualResetEvent:" & vbLf)
For i As Integer = 0 To 2
Dim t As New Thread(AddressOf ThreadProc)
t.Name = "Thread_" & i
t.Start()
Next i
Thread.Sleep(500)
Console.WriteLine(vbLf & _
"When all three threads have started, press Enter to call Set()" & vbLf & _
"to release all the threads." & vbLf)
Console.ReadLine()
mre.Set()
Thread.Sleep(500)
Console.WriteLine(vbLf & _
"When a ManualResetEvent is signaled, threads that call WaitOne()" & vbLf & _
"do not block. Press Enter to show this." & vbLf)
Console.ReadLine()
For i As Integer = 3 To 4
Dim t As New Thread(AddressOf ThreadProc)
t.Name = "Thread_" & i
t.Start()
Next i
Thread.Sleep(500)
Console.WriteLine(vbLf & _
"Press Enter to call Reset(), so that threads once again block" & vbLf & _
"when they call WaitOne()." & vbLf)
Console.ReadLine()
mre.Reset()
' Start a thread that waits on the ManualResetEvent.
Dim t5 As New Thread(AddressOf ThreadProc)
t5.Name = "Thread_5"
t5.Start()
Thread.Sleep(500)
Console.WriteLine(vbLf & "Press Enter to call Set() and conclude the demo.")
Console.ReadLine()
mre.Set()
' If you run this example in Visual Studio, uncomment the following line:
'Console.ReadLine()
End Sub
Private Shared Sub ThreadProc()
Dim name As String = Thread.CurrentThread.Name
Console.WriteLine(name & " starts and calls mre.WaitOne()")
mre.WaitOne()
Console.WriteLine(name & " ends.")
End Sub
End Class
' This example produces output similar to the following:
'
'Start 3 named threads that block on a ManualResetEvent:
'
'Thread_0 starts and calls mre.WaitOne()
'Thread_1 starts and calls mre.WaitOne()
'Thread_2 starts and calls mre.WaitOne()
'
'When all three threads have started, press Enter to call Set()
'to release all the threads.
'
'
'Thread_2 ends.
'Thread_0 ends.
'Thread_1 ends.
'
'When a ManualResetEvent is signaled, threads that call WaitOne()
'do not block. Press Enter to show this.
'
'
'Thread_3 starts and calls mre.WaitOne()
'Thread_3 ends.
'Thread_4 starts and calls mre.WaitOne()
'Thread_4 ends.
'
'Press Enter to call Reset(), so that threads once again block
'when they call WaitOne().
'
'
'Thread_5 starts and calls mre.WaitOne()
'
'Press Enter to call Set() and conclude the demo.
'
'Thread_5 ends.
설명
스레드 상호 작용(또는 스레드 신호)을 사용합니다ManualResetEventAutoResetEventEventWaitHandle. 자세한 내용은 동기화 기본 형식 개요 문서의 스레드 상호 작용 또는 신호 섹션을 참조하세요.
스레드가 다른 스레드를 계속 진행하기 전에 완료해야 하는 작업을 시작하면 ManualResetEvent.Reset 을 호출하여 ManualResetEvent 신호가 없는 상태로 전환합니다. 이 스레드는 을 제어하는 ManualResetEvent것으로 간주할 수 있습니다.
신호를 기다리는 ManualResetEvent.WaitOne 블록을 호출하는 스레드입니다. 제어 스레드가 작업을 완료하면 ManualResetEvent.Set 를 호출하여 대기 중인 스레드가 진행될 수 있음을 알릴 수 있습니다. 대기 중인 모든 스레드가 해제됩니다.
신호를 받으면 메서드를 ManualResetEvent 호출하여 수동으로 다시 설정될 때까지 신호를 유지합니다 Reset() . 즉, 즉시 반환을 호출합니다 WaitOne .
초기 상태가 ManualResetEvent 신호를 받으면 생성자에 true 부울 값을 전달하여 초기 상태를 제어할 수 있습니다 false .
ManualResetEvent는 및 static 메서드와 함께 WaitAllWaitAny 사용할 수도 있습니다.
.NET Framework 버전 2.0부터 ManualResetEventEventWaitHandle 클래스에서 파생됩니다. A ManualResetEvent 는 .를 사용하여 만든 EventWaitHandle것과 EventResetMode.ManualReset 기능적으로 동일합니다.
메모
클래스와 ManualResetEvent 달리 클래스는 EventWaitHandle 명명된 시스템 동기화 이벤트에 대한 액세스를 제공합니다.
.NET Framework 버전 4.0부터 System.Threading.ManualResetEventSlim 클래스는 ManualResetEvent 대한 간단한 대안입니다.
생성자
| Name | Description |
|---|---|
| ManualResetEvent(Boolean) |
초기 상태를 신호로 설정할지 여부를 나타내는 부울 값을 사용하여 클래스의 새 인스턴스 ManualResetEvent 를 초기화합니다. |
필드
| 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 개체가 나타내는 명명된 시스템 이벤트에 대한 액세스 제어 보안을 나타내는 개체를 가져옵니다. (다음에서 상속됨 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, 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) |
네이티브 운영 체제 대기 핸들에 대한 안전 핸들을 설정합니다. |
적용 대상
스레드 보안
이 클래스는 스레드로부터 안전합니다.
추가 정보
- WaitHandle
- 관리되는 스레딩
- 동기화 기본 형식 개요