AutoResetEvent(Boolean) 생성자
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
초기 상태를 신호로 설정할지 여부를 나타내는 부울 값을 사용하여 클래스의 새 인스턴스 AutoResetEvent 를 초기화합니다.
public:
AutoResetEvent(bool initialState);
public AutoResetEvent(bool initialState);
new System.Threading.AutoResetEvent : bool -> System.Threading.AutoResetEvent
Public Sub New (initialState As Boolean)
매개 변수
- initialState
- Boolean
예제
다음 예제에서는 두 스레드의 활동을 동기화하는 데 사용합니다 AutoResetEvent . 애플리케이션 스레드인 첫 번째 스레드가 실행됩니다 Main.
static(Visual Basic Shared) 필드인 number 보호된 리소스에 값을 씁니다. 두 번째 스레드는 에 의해 ThreadProc작성된 값을 읽는 정적 Main 메서드를 실행합니다.
메서드는 ThreadProc .를 AutoResetEvent대기합니다. 메서드를 호출할 MainSet때 AutoResetEvent 메서드는 ThreadProc 하나의 값을 읽습니다.
AutoResetEvent 즉시 다시 설정되므로 메서드가 ThreadProc 다시 대기합니다.
프로그램 논리는 메서드가 ThreadProc 동일한 값을 두 번 읽지 않도록 보장합니다. 메서드가 ThreadProc 로 작성된 Main모든 값을 읽는 것을 보장하지는 않습니다. 이 보장에는 두 번째 AutoResetEvent 잠금이 필요합니다.
각 쓰기 작업 Main 후에 메서드를 Thread.Sleep 호출하여 생성하여 두 번째 스레드를 실행할 기회를 제공합니다. 그렇지 않으면 단일 프로세서 컴퓨터 Main 에서 두 읽기 작업 간에 많은 값을 씁니다.
using System;
using System.Threading;
namespace AutoResetEvent_Examples
{
class MyMainClass
{
//Initially not signaled.
const int numIterations = 100;
static AutoResetEvent myResetEvent = new AutoResetEvent(false);
static int number;
static void Main()
{
//Create and start the reader thread.
Thread myReaderThread = new Thread(new ThreadStart(MyReadThreadProc));
myReaderThread.Name = "ReaderThread";
myReaderThread.Start();
for(int i = 1; i <= numIterations; i++)
{
Console.WriteLine("Writer thread writing value: {0}", i);
number = i;
//Signal that a value has been written.
myResetEvent.Set();
//Give the Reader thread an opportunity to act.
Thread.Sleep(1);
}
//Terminate the reader thread.
myReaderThread.Abort();
}
static void MyReadThreadProc()
{
while(true)
{
//The value will not be read until the writer has written
// at least once since the last read.
myResetEvent.WaitOne();
Console.WriteLine("{0} reading value: {1}", Thread.CurrentThread.Name, number);
}
}
}
}
Imports System.Threading
Namespace AutoResetEvent_Examples
Class MyMainClass
'Initially not signaled.
Private Const numIterations As Integer = 100
Private Shared myResetEvent As New AutoResetEvent(False)
Private Shared number As Integer
<MTAThread> _
Shared Sub Main()
'Create and start the reader thread.
Dim myReaderThread As New Thread(AddressOf MyReadThreadProc)
myReaderThread.Name = "ReaderThread"
myReaderThread.Start()
Dim i As Integer
For i = 1 To numIterations
Console.WriteLine("Writer thread writing value: {0}", i)
number = i
'Signal that a value has been written.
myResetEvent.Set()
'Give the Reader thread an opportunity to act.
Thread.Sleep(1)
Next i
'Terminate the reader thread.
myReaderThread.Abort()
End Sub
Shared Sub MyReadThreadProc()
While True
'The value will not be read until the writer has written
' at least once since the last read.
myResetEvent.WaitOne()
Console.WriteLine("{0} reading value: {1}", Thread.CurrentThread.Name, number)
End While
End Sub
End Class
End Namespace 'AutoResetEvent_Examples
적용 대상
추가 정보
- WaitHandle
- 관리되는 스레딩
- 동기화 기본 형식 개요