Interface to an Async result.
interface IAsyncResult { public boolean IsCompleted() { get; } public boolean CompletedSynchronously() { get; } public WaitHandle AsyncWaitHandle () { get; } public Object AsyncObject () { get; } public Object AsyncObject () { get; } }
IsCompleted. The IsCompleted property will be set to “true” after the server has completed processing of the call. It is illegal for the server to use any client supplied resources outside of the agreed upon sharing semantics after it sets the IsCompleted property to “true”. Thus, it is safe for the client to destroy the resources after IsCompleted property returns “true”.
CompletedSynchronously. The CompletedSynchronously property will be set to “true” if the BeginXXXXXX call completed synchronously. If this is detected in the AsyncCallback delegate, it is probable that the thread that called BeginInvoke is the current thread. Most providers of the IAsyncResult interface will not use the capability and will return a default false.
AsyncWaitHandle. The AsyncWaitHandle property returns the WaitHandle that can use to perform a WaitHandle.WaitOne or WaitAny or WaitAll. The object which implements IAsyncResult need not derive from the System.WaitHandle classes directly. The WaitHandle wraps its underlying synchronization primitive and should be signaled after the call is completed. This enables the client to wait for the call to complete instead polling. The Runtime supplies a number of waitable objects that mirror Win32 synchronization primitives e.g. ManualResetEvent, AutoResetEvent and Mutex.
WaitHandle supplies methods that support waiting for such synchronization objects to become signaled with “any” or “all” semantics i.e. WaitHandle.WaitOne, WaitAny and WaitAll. Such methods are context aware to avoid deadlocks. The AsyncWaitHandle can be allocated eagerly or on demand. It is the choice of the IAsyncResult implementer.
Implementors of classes that return IAsyncResult should note the following: The AsyncWaitHandle can be lazily allocated. However once allocated it should be kept alive until the user calls EndXXXX. At that time the object behind AsyncWaitHandle can be recycled.
AsyncObject. The AsyncObject returns the object that provided the IAsyncResult as part of the BeginXXXX method call. When BeginInvoke is called on the delegate and returns IAsyncResult, AsyncObject will contain for delegate object. For mechanisms such as file IO the AsyncObject could contain the file object on which the BeginXXXX method was called.
AsyncObject. The AsyncObject returns the object that was provided as the last parameter as part of the BeginXXXX method call.
Delegate for methods that complete asynchronously.
public delegate void AsyncCallback(IAsyncResult ar);