Export (0) Print
Expand All

AsyncResult Class

Encapsulates the results of an asynchronous operation on an asynchronous delegate.

Namespace: System.Runtime.Remoting.Messaging
Assembly: mscorlib (in mscorlib.dll)

[ComVisibleAttribute(true)] 
public class AsyncResult : IAsyncResult, IMessageSink
/** @attribute ComVisibleAttribute(true) */ 
public class AsyncResult implements IAsyncResult, IMessageSink
ComVisibleAttribute(true) 
public class AsyncResult implements IAsyncResult, IMessageSink

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 AsyncDelegate property that holds the delegate object on which the async call was invoked.

For more information about BeginInvoke and asynchronous delegates, see Asynchronous Programming Using Delegates.

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);
    }
}

import System.*;
import System.Threading.*;
import System.Runtime.Remoting.*;
import System.Runtime.Remoting.Contexts.*;
import System.Runtime.Remoting.Messaging.*;

// Context-bound type with the Synchronization context attribute.
/** @attribute Synchronization()
 */
public class SampleSyncronized extends 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}",
            (Int32)System.Threading.Thread.get_CurrentThread().GetHashCode());
        return i * i;
    } //Square
} //SampleSyncronized

// The async delegate used to call a method with this signature asynchronously.
/** @delegate 
 */
public delegate int SampSyncSqrDelegate(int i);

public class AsyncResultSample
{
    static int value;
    // Asynchronous Callback method.
    public static void MyCallback(IAsyncResult ar)
    {
        // Obtains the last parameter of the delegate call.
        SampSyncSqrDelegate sampDelg = (SampSyncSqrDelegate)ar.get_AsyncState();
        
        // Obtains return value from the delegate call using EndInvoke.
        AsyncResult aResult = (AsyncResult)ar;
        SampSyncSqrDelegate temp =
            (SampSyncSqrDelegate)(aResult.get_AsyncDelegate());
        int threadId = AppDomain.GetCurrentThreadId();
        int result = temp.EndInvoke(ar);

        Console.Write("Simple.SomeMethod (AsyncCallback): Result of ");
        Console.WriteLine("{0} in SampleSynchronized.Square is {1} ",
            (Int32)value, (Int32)result);
    } //MyCallback

    public static void main(String[] args)
    {
        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}.", (Int32)param, (Int32)result);
        Console.WriteLine("");

        SampSyncSqrDelegate sampleDelegate =
            new SampSyncSqrDelegate(sampSyncObj.Square);
        param = 8;
        value = param;
        Console.WriteLine("Making a single asynchronous call on the "
            + "context-bound object:");
        IAsyncResult ar1 = sampleDelegate.BeginInvoke(param,
            new AsyncCallback(AsyncResultSample.MyCallback), sampleDelegate);

        Console.WriteLine("Waiting for the asynchronous call to complete...");
        WaitHandle wh = ar1.get_AsyncWaitHandle();

        wh.WaitOne();
        Console.WriteLine("");
        Console.WriteLine("Waiting for the AsyncCallback to complete...");
        System.Threading.Thread.Sleep(1000);
    } //main 
} //AsyncResultSample

System.Object
  System.Runtime.Remoting.Messaging.AsyncResult

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft