System.Threading.WaitHandle.SignalAndWait Method

Signals one System.Threading.WaitHandle and waits on another, specifying the time-out interval as a TimeSpan and specifying whether to exit the synchronization domain for the context before entering the wait.

Syntax

public static bool SignalAndWait (WaitHandle toSignal, WaitHandle toWaitOn, TimeSpan timeout, bool exitContext)

Parameters

toSignal
The System.Threading.WaitHandle to signal.
toWaitOn
The System.Threading.WaitHandle to wait on.
timeout
A TimeSpan that represents the interval to wait. If the value is -1, the wait is infinite.
exitContext
true to exit the synchronization domain for the context before the wait (if in a synchronized context), and reacquire it afterward; otherwise, false.

Returns

true if both the signal and the wait completed successfully, or false if the signal completed but the wait timed out.

Remarks

This operation is not guaranteed to be atomic. After the current thread signals toSignal but before it waits on toWaitOn, a thread that is running on another processor might signal toWaitOn or wait on it.

The maximum value for timeout is int.MaxValue.

If timeout is zero, the method does not block. It tests the state of the toWaitOn and returns immediately.

Notes on Exiting the Context

The exitContext parameter has no effect unless the WaitHandle.SignalAndWait(WaitHandle, WaitHandle, TimeSpan, bool) method is called from inside a nondefault managed context. This can happen if your thread is inside a call to an instance of a class derived from ContextBoundObject. Even if you are currently executing a method on a class that does not derive from ContextBoundObject, like string, you can be in a nondefault context if a ContextBoundObject is on your stack in the current application domain.

When your code is executing in a nondefault context, specifying true for exitContext causes the thread to exit the nondefault managed context (that is, to transition to the default context) before executing the WaitHandle.SignalAndWait(WaitHandle, WaitHandle, TimeSpan, bool) method. The thread returns to the original nondefault context after the call to the WaitHandle.SignalAndWait(WaitHandle, WaitHandle, TimeSpan, bool) method completes.

This can be useful when the context-bound class has System.Runtime.Remoting.Contexts.SynchronizationAttribute. In that case, all calls to members of the class are automatically synchronized, and the synchronization domain is the entire body of code for the class. If code in the call stack of a member calls the WaitHandle.SignalAndWait(WaitHandle, WaitHandle, TimeSpan, bool) method and specifies true for exitContext, the thread exits the synchronization domain, allowing a thread that is blocked on a call to any member of the object to proceed. When the WaitHandle.SignalAndWait(WaitHandle, WaitHandle, TimeSpan, bool) method returns, the thread that made the call must wait to reenter the synchronization domain.

Requirements

Namespace: System.Threading
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 2.0.0.0, 4.0.0.0
Since: .NET 2.0