AutoResetEvent 클래스
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
신호를 받으면 대기 중인 단일 스레드를 해제한 다음 자동으로 다시 설정하는 스레드 동기화 이벤트를 나타냅니다. 이 클래스는 상속할 수 없습니다.
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
- 상속
- 상속
- 상속
- 특성
예제
다음 예제에서는 사용자가 Enter 키를 누를 때마다 메서드를 호출 Set 하여 한 번에 하나의 스레드를 해제하는 방법을 AutoResetEvent 보여 줍니다. 이 예제에서는 신호된 상태에서 생성된 스레드를 AutoResetEvent 기다리는 세 개의 스레드를 시작합니다. 첫 번째 스레드는 이미 신호를 받은 상태이므로 즉시 AutoResetEvent 해제됩니다. 그러면 신호가 AutoResetEvent 없는 상태로 다시 설정되므로 후속 스레드가 차단됩니다. 차단된 스레드는 사용자가 Enter 키를 눌러 한 번에 하나씩 해제할 때까지 해제되지 않습니다.
스레드가 첫 번째 AutoResetEvent스레드에서 해제된 후 신호가 없는 상태로 만들어진 다른 AutoResetEvent 스레드를 기다립니다. 세 개의 스레드가 모두 차단되므로 메서드를 Set 모두 해제하려면 세 번 호출해야 합니다.
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.
설명
스레드 상호 작용(또는 스레드 신호)을 사용합니다AutoResetEventManualResetEventEventWaitHandle. 자세한 내용은 스레드 상호 작용을 참조하세요.
스레드는 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.AutoReset것과 EventWaitHandle 기능적으로 동일합니다.
메모
클래스와 AutoResetEvent 달리 클래스는 EventWaitHandle 명명된 시스템 동기화 이벤트에 대한 액세스를 제공합니다.
중요하다
이 형식은 IDisposable 인터페이스를 구현합니다. 형식 사용을 마쳤으면 직접 또는 간접적으로 삭제해야 합니다. 형식을 직접 삭제하려면 Disposetry/ 블록에서 해당 catch 메서드를 호출합니다. 간접적으로 삭제하려면 using(C#) 또는 Using(Visual Basic)와 같은 언어 구문을 사용합니다. 자세한 내용은 인터페이스 페이지의 "IDisposable을 구현하는 개체 사용" 섹션을 IDisposable 참조하세요.
생성자
| Name | Description |
|---|---|
| AutoResetEvent(Boolean) |
초기 상태를 신호로 설정할지 여부를 나타내는 부울 값을 사용하여 클래스의 새 인스턴스 AutoResetEvent 를 초기화합니다. |
필드
| 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
- 관리되는 스레딩
- 동기화 기본 형식 개요