Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

AsyncResult.AsyncWaitHandle Property

Gets a WaitHandle that encapsulates Win32 synchronization handles, and allows the implementation of various synchronization schemes.

Namespace: System.Runtime.Remoting.Messaging
Assembly: mscorlib (in mscorlib.dll)

public virtual WaitHandle AsyncWaitHandle { get; }
/** @property */
public WaitHandle get_AsyncWaitHandle ()

public function get AsyncWaitHandle () : WaitHandle

Property Value

A WaitHandle that encapsulates Win32 synchronization handles, and allows the implementation of various synchronization schemes.

The WaitHandle returned by this method is automatically signaled when the asynchronous operation has completed.

An object that implements IAsyncResult does not need to derive directly from the WaitHandle classes. WaitHandle wraps its synchronization primitive and must be signaled after the call is completed. This enables the client to wait for the call to complete instead polling for call status. The common language runtime supplies a number of waitable objects that mirror Win32 synchronization primitives such as ManualResetEvent, AutoResetEvent and Mutex.

WaitHandle supplies methods that support waiting for such synchronization objects to become signaled with the any or all semantics, that is WaitHandle.WaitOne, WaitAny and WaitAll. Such methods are context aware to avoid deadlocks. The AsyncResult.AsyncWaitHandle can be allocated eagerly or in lazy manner. It is the choice of the IAsyncResult implementer.

Implementers of classes that return IAsyncResult must note that the AsyncWaitHandle can be lazily allocated. Once allocated, however, it should be kept alive until the user calls EndInvoke. At that time the object behind AsyncWaitHandle can be recycled.

Caution noteCaution

The WaitHandle contained in the AsyncWaitHandle property can be used to block the current thread until the asynchronous call is complete. However the WaitHandle will ignore the AsyncCallback, if one was specified during the BeginInvoke call. Therefore, a situation can occur where the application shuts down before the AsyncCallback has finished executing, even if a WaitHandle is used to block until the asynchronous call completion. For an example of such a situation, see the example for the AsyncResult class, and remove the Thread.Sleep statement.

The following code example demonstrates how the AsyncWaitHandle property is used to get a WaitHandle for an asynchronous call on a delegate. For the complete example code, see the example for the AsyncResult class. Note that the WaitHandle is not explicitly signaled in this example. This is because implementors of IAsyncResult are responsible for signaling the WaitHandle obtained from this property.

SampSyncSqrDelegate sampleDelegate = new SampSyncSqrDelegate(sampSyncObj.Square);
param = 8;

Console.WriteLine("Making a single asynchronous call on the context-bound object:");

IAsyncResult ar1 = sampleDelegate.BeginInvoke( param, 
                      new AsyncCallback(AsyncResultSample.MyCallback), 
                      param);

Console.WriteLine("Waiting for the asynchronous call to complete...");
WaitHandle wh = ar1.AsyncWaitHandle;
wh.WaitOne();

Console.WriteLine("");
Console.WriteLine("Waiting for the AsyncCallback to complete...");
Thread.Sleep(1000);

SampSyncSqrDelegate sampleDelegate =
    new SampSyncSqrDelegate(sampSyncObj.Square);
param = 8;
value = param;
Console.WriteLine("Making a single asynchronous call on the "
    + "context-bound object:");
IAsyncResult ar1 = sampleDelegate.BeginInvoke(param,
    new AsyncCallback(AsyncResultSample.MyCallback), sampleDelegate);

Console.WriteLine("Waiting for the asynchronous call to complete...");
WaitHandle wh = ar1.get_AsyncWaitHandle();

wh.WaitOne();
Console.WriteLine("");
Console.WriteLine("Waiting for the AsyncCallback to complete...");
System.Threading.Thread.Sleep(1000);

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft