AutoResetEvent(Boolean) 생성자

정의

초기 상태를 신호 받음으로 설정할지 여부를 나타내는 부울 값을 사용하여 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

초기 상태를 신호 받음으로 설정하려면 true를 사용하고 초기 상태를 신호 없음으로 설정하려면 false를 사용합니다.

예제

다음 예제에서는 두 스레드의 활동을 동기화하는 데 사용합니다 AutoResetEvent . 애플리케이션 스레드에 인 첫 번째 스레드가 실행 Main합니다. Visual Basic 필드number인 보호된 리소스 static``Shared 에 값을 씁니다. 두 번째 스레드는 정적 ThreadProc 메서드를 실행합니다. 이 메서드는 .에서 작성한 Main값을 읽습니다.

메서드는 ThreadProc .을 AutoResetEvent대기합니다. 메서드에서 Set 메서드를 호출할 AutoResetEventMain 메서드는 ThreadProc 하나의 값을 읽습니다. AutoResetEvent 즉시 다시 설정되므로 메서드가 ThreadProc 다시 대기합니다.

프로그램 논리는 메서드가 ThreadProc 동일한 값을 두 번 읽지 않도록 보장합니다. 메서드가 .로 작성된 Main모든 값을 읽는 것을 ThreadProc 보장하지는 않습니다. 이 보장에는 두 번째 AutoResetEvent 잠금이 필요합니다.

각 쓰기 작업 Main 후에 메서드를 Thread.Sleep 호출하여 생성하여 두 번째 스레드를 실행할 기회를 제공합니다. 그렇지 않으면 단일 프로세서 컴퓨터 Main 에서 두 읽기 작업 간에 많은 값을 씁니다.

using namespace System;
using namespace System::Threading;
ref class MyMainClass
{
public:
   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 );
      }
   }


   //Initially not signaled.
   static AutoResetEvent^ myResetEvent = gcnew AutoResetEvent( false );
   static int number;
   literal int numIterations = 100;
};

int main()
{
   
   //Create and start the reader thread.
   Thread^ myReaderThread = gcnew Thread( gcnew ThreadStart( MyMainClass::MyReadThreadProc ) );
   myReaderThread->Name = "ReaderThread";
   myReaderThread->Start();
   for ( int i = 1; i <= MyMainClass::numIterations; i++ )
   {
      Console::WriteLine( "Writer thread writing value: {0}", i );
      MyMainClass::number = i;
      
      //Signal that a value has been written.
      MyMainClass::myResetEvent->Set();
      
      //Give the Reader thread an opportunity to act.
      Thread::Sleep( 1 );

   }
   
   //Terminate the reader thread.
   myReaderThread->Abort();
}
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

적용 대상

추가 정보