System.Runtime.Remoting.Messaging.AsyncResult Class

Assembly: Mscorlib.dll
Namespace: System.Runtime.Remoting.Messaging
Summary
Encapsulates the results of an asynchronous operation on an asynchronous delegate.
C# Syntax:
public class AsyncResult : IAsyncResult, IMessageSink
Remarks
The AsyncResult class is used in conjunction with asynchronous delegates. The IAsyncResult returned from the delegate's BeginInvoke method can be cast to an AsyncResult. The AsyncResult has the AsyncResult.AsyncDelegate property that holds the delegate object on which the async call was invoked.

For more information about BeginInvoke and asynchronous delegates, see .

Example
The following code example demonstrates the use of the AsyncResult class to retrieve the results of an asynchronous operation on an asynchronous delegate.
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);
    }
}

    
See also:
System.Runtime.Remoting.Messaging Namespace

System.Runtime.Remoting.Messaging.AsyncResult Member List:

Public Properties
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.
Public Methods
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.
Protected Methods
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:


System.Runtime.Remoting.Messaging.AsyncResult Member Details

Property: AsyncDelegate (read-only)
Summary
Gets the delegate object on which the asynchronous call was invoked.
C# Syntax:
public virtual object AsyncDelegate {get;}
Remarks
The AsyncResult.AsyncDelegate property can be cast to the actual class of the user-defined delegate.

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 .

Example
The following code example demonstrates how to get the delegate object on which the asynchronous call was invoked from the AsyncResult. For the complete example code, see the example for the AsyncResult class.
    // 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);
    }

    

Return to top


Property: AsyncState (read-only)
Summary
Gets the object provided as the last parameter of a BeginInvoke method call.
C# Syntax:
public virtual object AsyncState {get;}
Implements:
IAsyncResult.AsyncState
Remarks
For more information about the BeginInvoke method, see .
Example
The following code example demonstrates how the AsyncResult.AsyncState property is used to get the last parameter of a BeginInvoke method call. For the complete example code, see the example for the AsyncResult class.
    // 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);
    }

    

Return to top


Property: AsyncWaitHandle (read-only)
Summary
Gets a WaitHandle that encapsulates Win32 synchronization handles, and allows the implementation of various synchronization schemes.
C# Syntax:
public virtual WaitHandle AsyncWaitHandle {get;}
Implements:
IAsyncResult.AsyncWaitHandle
Remarks
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, 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.
Example
The following code example demonstrates how the AsyncResult.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.
        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);

    
See also:
WaitHandle

Return to top


Property: CompletedSynchronously (read-only)
Summary
Gets a value indicating whether the BeginInvoke call completed synchronously.
C# Syntax:
public virtual bool CompletedSynchronously {get;}
Implements:
IAsyncResult.CompletedSynchronously
Remarks
If it is detected that the BeginInvoke call completed synchronously 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.

Current implementation of AsyncResult.CompletedSynchronously always returns false.

Return to top


Property: EndInvokeCalled (read-write)
Summary
Gets or sets a value indicating whether EndInvoke has been called on the current AsyncResult.
C# Syntax:
public bool EndInvokeCalled {get; set;}

Return to top


Property: IsCompleted (read-only)
Summary
Gets a value indicating whether the server has completed the call.
C# Syntax:
public virtual bool IsCompleted {get;}
Implements:
IAsyncResult.IsCompleted
Remarks
The server must not use any client supplied resources outside of the agreed upon sharing semantics after it sets the AsyncResult.IsCompleted property to true. Thus, it is safe for the client to destroy the resources after the AsyncResult.IsCompleted property returns true.

Return to top


Property: NextSink (read-only)
Summary
Public reserved. Gets the next message sink in the sink chain.
C# Syntax:
public IMessageSink NextSink {get;}
Implements:
IMessageSink.NextSink
Remarks
The current implementation of AsyncResult.NextSink returns null.

Return to top


Method: AsyncProcessMessage(
   IMessage msg,
   IMessageSink replySink
)
Summary
Public reserved.
C# Syntax:
public virtual IMessageCtrl AsyncProcessMessage(
   IMessage msg,
   IMessageSink replySink
);
Parameters:

msg

The request IMessage.

replySink

The reply IMessageSink.

Return Value:
An IMessageCtrl.
Implements:
IMessageSink.AsyncProcessMessage

Return to top


Method: Equals(
   object obj
)
Inherited
See base class member description: System.Object.Equals
C# Syntax:
public virtual bool Equals(
   object obj
);

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~AsyncResult();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetHashCode()
Inherited
See base class member description: System.Object.GetHashCode
C# Syntax:
public virtual int GetHashCode();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetReplyMessage()
Summary
Public reserved. Returns the reply message returned from a method call.
C# Syntax:
public virtual IMessage GetReplyMessage();
Return Value:
The reply message returned from a method call.

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: SetMessageCtrl(
   IMessageCtrl mc
)
Summary
Public reserved. Sets the IMessageCtrl object for the current method call which provides a way to control asynchronous messages once they have dispatched.
C# Syntax:
public virtual void SetMessageCtrl(
   IMessageCtrl mc
);
Parameters:

mc

The IMessageCtrl object for the current method call.

Return to top


Method: SyncProcessMessage(
   IMessage msg
)
Summary
Public reserved.
C# Syntax:
public virtual IMessage SyncProcessMessage(
   IMessage msg
);
Parameters:

msg

Public reserved.

Return Value:
Public reserved.
Implements:
IMessageSink.SyncProcessMessage

Return to top


Method: ToString()
Inherited
See base class member description: System.Object.ToString
C# Syntax:
public virtual string ToString();

For more information on members inherited from System.Object click on the link above.

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.