AutoResetEvent Class

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

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

No code example is currently available or this language may not be supported.

The AutoResetEvent type exposes the following members.

  NameDescription
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360AutoResetEventInitializes a new instance of the AutoResetEvent class with a Boolean value that indicates whether to set the initial state to signaled.
Top

  NameDescription
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360SafeWaitHandleGets or sets the native operating-system handle. (Inherited from WaitHandle.)
Top

  NameDescription
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360CloseWhen overridden in a derived class, releases all resources held by the current WaitHandle. (Inherited from WaitHandle.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360Dispose()Releases all resources used by the current instance of the WaitHandle class. (Inherited from WaitHandle.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360Dispose(Boolean)When overridden in a derived class, releases the unmanaged resources used by the WaitHandle, and optionally releases the managed resources. (Inherited from WaitHandle.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360Equals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360FinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360GetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360GetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360MemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360ResetSets the state of the event to non-signaled, which causes threads to block. (Inherited from EventWaitHandle.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360SetSets the state of the event to signaled, which allows one or more waiting threads to proceed. (Inherited from EventWaitHandle.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360ToStringReturns a string that represents the current object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360WaitOne()Blocks the current thread until the current WaitHandle receives a signal. (Inherited from WaitHandle.)

In Silverlight for Windows Phone, this member is overridden by WaitOne().


In XNA Framework, this member is overridden by WaitOne().
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360WaitOne(Int32)Blocks the current thread until the current WaitHandle receives a signal, using 32-bit signed integer to specify the time interval. (Inherited from WaitHandle.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360WaitOne(TimeSpan)Blocks the current thread until the current instance receives a signal, using a TimeSpan to specify the time interval. (Inherited from WaitHandle.)
Top

AutoResetEvent enables 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 the WaitOne method on the AutoResetEvent. If the AutoResetEvent is in the non-signaled state, the thread blocks, waiting for the thread that currently controls the resource to signal that the resource is available by calling the Set method.

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 non-signaled state. If no threads are waiting, the state remains signaled indefinitely.

If a thread calls WaitOne while the AutoResetEvent is in the signaled state, the thread does not block. The AutoResetEvent releases the thread immediately and returns to the non-signaled state.

Important noteImportant Note:

There is no guarantee that every call to the Set method will release a thread. If two calls are so close together that the second call occurs before a thread has been released, only one thread is released. It is as if the second call did not occur. Also, if Set is called when there are no threads waiting and the AutoResetEvent is already signaled, the call has no effect.

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 WaitAll and WaitAny methods.

For more information about thread synchronization mechanisms, see EventWaitHandle, AutoResetEvent, and ManualResetEvent and Synchronization Primitives in the conceptual documentation.

This section contains two code examples. The first example uses an AutoResetEvent to synchronize the activities of a reader thread and a writer thread. The second example demonstrates the use of AutoResetEvent and ManualResetEvent to control the interaction of multiple threads.

Example 1

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

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

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

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

The Demo method starts the two thread procedures. The threads write their output to a StringBuilder object, which is protected from concurrent access by lock statements (SyncLock statements in Visual Basic). The MyWriteThreadProc method appends the final output to the TextBlock control by using the Dispatcher property to obtain a Dispatcher object for the TextBlock, and then using the Dispatcher::BeginInvoke method to make the cross-thread call to the UI thread.

No code example is currently available or this language may not be supported.

Example 2

The following example demonstrates how to use AutoResetEvent with ManualResetEvent to synchronize the activities of multiple threads, including the UI thread.

NoteNote:

For a more detailed description of this example, see the comments in the code or the full introduction to the example that is provided for the EventWaitHandle class.

An AutoResetEvent is used to synchronize the user interface thread with the DemoThread method that runs the example. The thread that executes the DemoThread method blocks on an AutoResetEvent between steps. In the MouseUp method, which handles the MouseLeftButtonUp event, the Set method is used to signal the AutoResetEvent, so that clicking the mouse runs the next step. AutoResetEvent is a good choice because it automatically resets to the non-signaled state.

AutoResetEvent is also used in the ThreadProc and ThreadProcARE thread procedures. In these cases, the AutoResetEvent is passed to the thread procedure as a parameter. The main thread, DemoThread, waits for the AutoResetEvent until ThreadProc or ThreadProcARE signals it to proceed by calling Set. This ensures that the threads in the example are coordinated, particularly with respect to the order in which they append messages to the TextBlock control that displays the output from the example.

No code example is currently available or this language may not be supported.

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

This class is thread safe.

Community Additions

ADD
Show: