.NET Framework Class Library  

AutoResetEvent Class

Notifies a waiting thread that an event has occurred. This class cannot be inherited.

For a list of all members of this type, see AutoResetEvent Members.

System.Object
   System.MarshalByRefObject
      System.Threading.WaitHandle
         System.Threading.AutoResetEvent

[Visual Basic]
NotInheritable Public Class AutoResetEvent
   Inherits WaitHandle
[C#]
public sealed class AutoResetEvent : WaitHandle
[C++]
public __gc __sealed class AutoResetEvent : public WaitHandle
[JScript]
public class AutoResetEvent extends WaitHandle

Thread Safety

This type is safe for multithreaded operations.

Remarks

AutoResetEvent allows threads to communicate with each other by signaling. Typically, this communication concerns a resource to which threads need exclusive access.

A thread waits for a signal by calling WaitOne on the AutoResetEvent. If the AutoResetEvent is in the nonsignaled state, the thread blocks, waiting for the thread that currently controls the resource to signal that the resource is available by calling Set.

Calling Set signals AutoResetEvent to release a waiting thread. AutoResetEvent remains signaled until a single waiting thread is released, and then automatically returns to the nonsignaled state. If no threads are waiting, the state remains signaled indefinitely.

You can control the initial state of an AutoResetEvent by passing a Boolean value to the constructor, true if the initial state is signaled and false otherwise.

AutoResetEvent can also be used with the static (Shared in Visual Basic) WaitAll and WaitAny methods.

For more information about thread synchronization mechanisms, see AutoResetEvent in the conceptual documentation.

Example

[Visual Basic] 
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

        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(0)
            Next i

            'Terminate the reader thread.
            myReaderThread.Abort()
        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.
                myResetEvent.WaitOne()
                Console.WriteLine("{0} reading value: {1}", Thread.CurrentThread.Name, number)
            End While
        End Sub 'MyReadThreadProc
    End Class 'MyMainClass
End Namespace 'AutoResetEvent_Examples

[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(0);
         }

         //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);
         }
      }
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::Threading;

__gc 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(S" {0} reading value: {1}", Thread::CurrentThread->Name, __box(number));
        }
    }
public:
    //Initially not signaled.
    static AutoResetEvent* myResetEvent = new AutoResetEvent(false);
    static int number;
    static const int numIterations = 100;
};

void main() 
{
    //Create and start the reader thread.
    Thread* myReaderThread = new Thread(new ThreadStart(0, MyMainClass::MyReadThreadProc));
    myReaderThread->Name = S"ReaderThread";
    myReaderThread->Start();

    for (int i = 1; i <= MyMainClass::numIterations; i++) 
    {
        Console::WriteLine(S"Writer thread writing value: {0}", __box(i));
        MyMainClass::number = i;

        //Signal that a value has been written.
        MyMainClass::myResetEvent->Set();

        //Give the Reader thread an opportunity to act.
        Thread::Sleep(0);
    }

    //Terminate the reader thread.
    myReaderThread->Abort();
}


[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Threading

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework - Windows CE .NET

Assembly: Mscorlib (in Mscorlib.dll)

See Also

AutoResetEvent Members | System.Threading Namespace | WaitHandle | Threading | AutoResetEvent