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.

[Visual Basic]
Public Overridable ReadOnly Property AsyncWaitHandle As WaitHandle  _
   Implements IAsyncResult.AsyncWaitHandle
[C#]
public virtual WaitHandle AsyncWaitHandle {get;}
[C++]
public: __property virtual WaitHandle* get_AsyncWaitHandle();
[JScript]
public function get AsyncWaitHandle() : WaitHandle;

Property Value

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

Implements

IAsyncResult.AsyncWaitHandle

Remarks

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   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.

Example

[Visual Basic, C#, C++] 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.

[Visual Basic] 
Dim sampleDelegate As New SampSyncSqrDelegate(AddressOf sampSyncObj.Square)
param = 8

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

Dim ar1 As IAsyncResult = sampleDelegate.BeginInvoke(param, New AsyncCallback(AddressOf AsyncResultSample.MyCallback), param)

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

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

[C#] 
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);

[C++] 
SampSyncSqrDelegate* sampleDelegate = new SampSyncSqrDelegate(sampSyncObj, SampleSyncronized::Square);
param = 8;

Console::WriteLine(S"Making a single asynchronous call on the context-bound Object*:");

IAsyncResult* ar1 = sampleDelegate->BeginInvoke(param, 
    new AsyncCallback(0, AsyncResultSample::MyCallback), 
    __box(param));

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

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

return 0;

[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

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

AsyncResult Class | AsyncResult Members | System.Runtime.Remoting.Messaging Namespace | WaitHandle

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