public class AsyncResult : IAsyncResult, IMessageSink
|
For more information about BeginInvoke and asynchronous delegates, see .
using System; using System.Threading; using System.Runtime.Remoting; using System.Runtime.Remoting.Contexts; using System.Runtime.Remoting.Messaging; // Context-bound type with the Synchronization context attribute. [Synchronization()] public class SampleSyncronized : ContextBoundObject { // A method that does some work, and returns the square of the given number. public int Square(int i) { Console.Write("The hash of the thread executing "); Console.WriteLine("SampleSyncronized.Square is: {0}", Thread.CurrentThread.GetHashCode()); return i*i; } } // The async delegate used to call a method with this signature asynchronously. public delegate int SampSyncSqrDelegate(int i); public class AsyncResultSample { // 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); } public static void Main() { int result; int param; // Creates an instance of a context-bound type SampleSynchronized. SampleSyncronized sampSyncObj = new SampleSyncronized(); // Checks whether the object is a proxy, since it is context-bound. if (RemotingServices.IsTransparentProxy(sampSyncObj)) Console.WriteLine("sampSyncObj is a proxy."); else Console.WriteLine("sampSyncObj is NOT a proxy."); param = 10; Console.WriteLine(""); Console.WriteLine("Making a synchronous call on the context-bound object:"); result = sampSyncObj.Square(param); Console.Write("The result of calling sampSyncObj.Square with "); Console.WriteLine("{0} is {1}.", param, result); Console.WriteLine(""); 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); } }
AsyncDelegate | Read-only Gets the delegate object on which the asynchronous call was invoked. |
AsyncState | Read-only Gets the object provided as the last parameter of a BeginInvoke method call. |
AsyncWaitHandle | Read-only Gets a WaitHandle that encapsulates Win32 synchronization handles, and allows the implementation of various synchronization schemes. |
CompletedSynchronously | Read-only Gets a value indicating whether the BeginInvoke call completed synchronously. |
EndInvokeCalled | Read-write Gets or sets a value indicating whether EndInvoke has been called on the current AsyncResult. |
IsCompleted | Read-only Gets a value indicating whether the server has completed the call. |
NextSink | Read-only Public reserved. Gets the next message sink in the sink chain. |
AsyncProcessMessage | Public reserved. |
Equals (inherited from System.Object) |
See base class member description: System.Object.Equals Derived from System.Object, the primary base class for all objects. |
GetHashCode (inherited from System.Object) |
See base class member description: System.Object.GetHashCode Derived from System.Object, the primary base class for all objects. |
GetReplyMessage | Public reserved. Returns the reply message returned from a method call. |
GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
SetMessageCtrl | Public reserved. Sets the IMessageCtrl object for the current method call which provides a way to control asynchronous messages once they have dispatched. |
SyncProcessMessage | Public reserved. |
ToString (inherited from System.Object) |
See base class member description: System.Object.ToString Derived from System.Object, the primary base class for all objects. |
Finalize (inherited from System.Object) |
See base class member description: System.Object.Finalize Derived from System.Object, the primary base class for all objects. |
MemberwiseClone (inherited from System.Object) |
See base class member description: System.Object.MemberwiseClone Derived from System.Object, the primary base class for all objects. |
Hierarchy:
public virtual object AsyncDelegate {get;}
|
For example, if the user-defined delegate is of type
MyDelegate
, in order to access
MyDelegate.EndInvoke
, the asynchronous delegate must be cast to
MyDelegate
. The
MyDelegate.EndInvoke
can be called in the async callback function (of type AsyncCallback) to obtain the results of the originally submitted
MyDelegate.BeginInvoke
.
// 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); }
public virtual object AsyncState {get;}
|
// 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); }
public virtual WaitHandle AsyncWaitHandle {get;}
|
WaitHandle supplies methods that support waiting for such synchronization objects to become signaled with the any or all semantics, that is WaitHandle.WaitOne, WaitHandle.WaitAny and WaitHandle.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 IAsyncResult.AsyncWaitHandle can be lazily allocated. Once allocated, however, it should be kept alive until the user calls EndInvoke. At that time the object behind IAsyncResult.AsyncWaitHandle can be recycled.
The WaitHandle contained in the AsyncResult.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.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);
public virtual bool CompletedSynchronously {get;}
|
Current implementation of AsyncResult.CompletedSynchronously always returns false.
public bool EndInvokeCalled {get; set;}
|
public virtual bool IsCompleted {get;}
|
public IMessageSink NextSink {get;}
|
public virtual IMessageCtrl AsyncProcessMessage( |
msg
replySink
~AsyncResult(); |
public virtual int GetHashCode(); |
public virtual IMessage GetReplyMessage(); |
public Type GetType(); |
protected object MemberwiseClone(); |
public virtual void SetMessageCtrl( |
mc
msg
public virtual string ToString(); |