Expand Minimize

EventResetMode Enumeration

Indicates whether an EventWaitHandle is reset automatically or manually after receiving a signal.

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

'Declaration
<ComVisibleAttribute(False)> _
Public Enumeration EventResetMode

Member nameDescription
Supported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsAutoResetWhen signaled, the EventWaitHandle resets automatically after releasing a single thread. If no threads are waiting, the EventWaitHandle remains signaled until a thread blocks, and resets after releasing the thread.
Supported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsManualResetWhen signaled, the EventWaitHandle releases all waiting threads and remains signaled until it is manually reset.

The following code example uses the SignalAndWait(WaitHandle, WaitHandle) method overload to allow the main thread to signal a blocked thread and then wait until the thread finishes a task.

The example starts five threads and allows them to block on an EventWaitHandle created with the EventResetMode.AutoReset flag, then releases one thread each time the user presses the ENTER key. The example then queues another five threads and releases them all using an EventWaitHandle created with the EventResetMode.ManualReset flag.

Imports System
Imports System.Threading

Public Class Example

    ' The EventWaitHandle used to demonstrate the difference 
    ' between AutoReset and ManualReset synchronization events. 
    
    Private Shared ewh As EventWaitHandle

    ' A counter to make sure all threads are started and 
    ' blocked before any are released. A Long is used to show 
    ' the use of the 64-bit Interlocked methods. 
    
    Private Shared threadCount As Long = 0

    ' An AutoReset event that allows the main thread to block 
    ' until an exiting thread has decremented the count. 
    
    Private Shared clearCount As New EventWaitHandle(False, _
        EventResetMode.AutoReset)

    <MTAThread> _
    Public Shared Sub Main()

        ' Create an AutoReset EventWaitHandle. 
        '
        ewh = New EventWaitHandle(False, EventResetMode.AutoReset)

        ' Create and start five numbered threads. Use the 
        ' ParameterizedThreadStart delegate, so the thread 
        ' number can be passed as an argument to the Start  
        ' method. 
        For i As Integer = 0 To 4
            Dim t As New Thread(AddressOf ThreadProc)
            t.Start(i)
        Next i

        ' Wait until all the threads have started and blocked. 
        ' When multiple threads use a 64-bit value on a 32-bit 
        ' system, you must access the value through the 
        ' Interlocked class to guarantee thread safety. 
        
        While Interlocked.Read(threadCount) < 5
            Thread.Sleep(500)
        End While 

        ' Release one thread each time the user presses ENTER, 
        ' until all threads have been released. 
        
        While Interlocked.Read(threadCount) > 0
            Console.WriteLine("Press ENTER to release a waiting thread.")
            Console.ReadLine()

            ' SignalAndWait signals the EventWaitHandle, which 
            ' releases exactly one thread before resetting,  
            ' because it was created with AutoReset mode.  
            ' SignalAndWait then blocks on clearCount, to  
            ' allow the signaled thread to decrement the count 
            ' before looping again. 
            '
            WaitHandle.SignalAndWait(ewh, clearCount)
        End While
        Console.WriteLine()

        ' Create a ManualReset EventWaitHandle. 
        '
        ewh = New EventWaitHandle(False, EventResetMode.ManualReset)

        ' Create and start five more numbered threads. 
        
        For i As Integer = 0 To 4
            Dim t As New Thread(AddressOf ThreadProc)
            t.Start(i)
        Next i

        ' Wait until all the threads have started and blocked. 
        
        While Interlocked.Read(threadCount) < 5
            Thread.Sleep(500)
        End While 

        ' Because the EventWaitHandle was created with 
        ' ManualReset mode, signaling it releases all the 
        ' waiting threads. 
        '
        Console.WriteLine("Press ENTER to release the waiting threads.")
        Console.ReadLine()
        ewh.Set()

    End Sub 

    Public Shared Sub ThreadProc(ByVal data As Object)
        Dim index As Integer = CInt(data)

        Console.WriteLine("Thread {0} blocks.", data)
        ' Increment the count of blocked threads.
        Interlocked.Increment(threadCount)

        ' Wait on the EventWaitHandle.
        ewh.WaitOne()

        Console.WriteLine("Thread {0} exits.", data)
        ' Decrement the count of blocked threads.
        Interlocked.Decrement(threadCount)

        ' After signaling ewh, the main thread blocks on 
        ' clearCount until the signaled thread has  
        ' decremented the count. Signal it now. 
        '
        clearCount.Set()
    End Sub 
End Class

.NET Framework

Supported in: 4.5.3, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft