public interface IAsyncResult
|
An object that supports the IAsyncResult interface stores state information for an asynchronous operation, and provides a synchronization object to allow threads to be signaled when the operation completes.
For a detailed description of how the IAsyncResult interface is used, see the topic.
// Asynchronous Callback method. public static void MyCallback(IAsyncResult ar) { // Obtains the last parameter of the delegate call. int value = Convert.ToInt32(ar.AsyncState); // Obtains return value from the delegate call using EndInvoke. AsyncResult aResult = (AsyncResult)ar; SampSyncSqrDelegate temp = (SampSyncSqrDelegate)aResult.AsyncDelegate; int result = temp.EndInvoke(ar); Console.Write("Simple.SomeMethod (AsyncCallback): Result of "); Console.WriteLine("{0} in SampleSynchronized.Square is {1} ", value, result); }
The following sample demonstrates waiting for an asynchronous operation to complete.
using System; using System.Threading; using System.Runtime.Remoting; using System.Runtime.Remoting.Contexts; using System.Runtime.Remoting.Messaging; // // Context-Bound type with Synchronization Context Attribute // [Synchronization()] public class SampleSyncronized : ContextBoundObject { // A method that does some work - returns the square of the given number public int Square(int i) { Console.Write("SampleSyncronized.Square called. "); Console.WriteLine("The hash of the current thread is: {0}", Thread.CurrentThread.GetHashCode()); return i*i; } } // // Async delegate used to call a method with this signature asynchronously // public delegate int SampSyncSqrDelegate(int i); //Main sample class public class AsyncResultSample { public static void Main() { int callParameter = 0; int callResult = 0; //Create an instance of a context-bound type SampleSynchronized //Because SampleSynchronized is context-bound, the object sampSyncObj //is a transparent proxy SampleSyncronized sampSyncObj = new SampleSyncronized(); //call the method synchronously Console.Write("Making a synchronous call on the object. "); Console.WriteLine("The hash of the current thread is: {0}", Thread.CurrentThread.GetHashCode()); callParameter = 10; callResult = sampSyncObj.Square(callParameter); Console.WriteLine("Result of calling sampSyncObj.Square with {0} is {1}.\n\n", callParameter, callResult); //call the method asynchronously Console.Write("Making an asynchronous call on the object. "); Console.WriteLine("The hash of the current thread is: {0}", Thread.CurrentThread.GetHashCode()); SampSyncSqrDelegate sampleDelegate = new SampSyncSqrDelegate(sampSyncObj.Square); callParameter = 17; IAsyncResult aResult = sampleDelegate.BeginInvoke(callParameter, null, null); //Wait for the call to complete aResult.AsyncWaitHandle.WaitOne(); callResult = sampleDelegate.EndInvoke(aResult); Console.WriteLine("Result of calling sampSyncObj.Square with {0} is {1}.", callParameter, callResult); } }
AsyncState | Read-only Gets a user-defined object that qualifies or contains information about an asynchronous operation. |
AsyncWaitHandle | Read-only Gets a WaitHandle that is used to wait for an asynchronous operation to complete. |
CompletedSynchronously | Read-only Gets an indication of whether the asynchronous operation completed synchronously. |
IsCompleted | Read-only Gets an indication whether the asynchronous operation has completed. |
object AsyncState {get;}
|
WaitHandle AsyncWaitHandle {get;}
|
The common language runtime supplies a number of waitable objects, such as ManualResetEvent, AutoResetEvent, and Mutex, all of which mirror Win32 synchronization primitives.
bool CompletedSynchronously {get;}
|
bool IsCompleted {get;}
|