AutoResetEvent Constructor (Boolean)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

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

Namespace:   System.Threading
Assembly:  mscorlib (in mscorlib.dll)

Public Sub New (
	initialState As Boolean


Type: System.Boolean

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

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.

Imports System
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"

            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.

                'Give the Reader thread an opportunity to act.
            Next i

            'Terminate the reader thread.
        End Sub 'Main

        Shared Sub MyReadThreadProc()
            While True
                'The value will not be read until the writer has written
                ' at least once since the last read.
                Console.WriteLine("{0} reading value: {1}", Thread.CurrentThread.Name, number)
            End While
        End Sub 'MyReadThreadProc
    End Class 'MyMainClass
End Namespace 'AutoResetEvent_Examples

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top