AutoResetEvent(Boolean) Constructor

Definition

Initializes a new instance of the AutoResetEvent class with a Boolean value indicating whether to set the initial state to signaled.

C#
public AutoResetEvent(bool initialState);

Parameters

initialState
Boolean

true to set the initial state to signaled; false to set the initial state to non-signaled.

Examples

The following example uses an AutoResetEvent to synchronize the activities of two threads. The first thread, which is the application thread, executes Main. It writes values to the protected resource, which is a static (Shared in Visual Basic) field named number. The second thread executes the static ThreadProc method, which reads the values written by Main.

The ThreadProc method waits for the AutoResetEvent. When Main calls the Set method on the AutoResetEvent, the ThreadProc method reads one value. The AutoResetEvent immediately resets, so the ThreadProc method waits again.

The program logic guarantees that the ThreadProc method will never read the same value two times. It does not guarantee that the ThreadProc method will read every value written by Main. That guarantee would require a second AutoResetEvent lock.

After each write operation, Main yields by calling the Thread.Sleep method, to give the second thread a chance to execute. Otherwise, on a single-processor computer Main would write many values between any two read operations.

C#
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);
         }
      }
    }
}

Applies to

Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

See also