IAsyncResult.AsyncState Property


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Gets a user-defined object that qualifies or contains information about an asynchronous operation.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

object AsyncState { get; }

Property Value

Type: System.Object

A user-defined object that qualifies or contains information about an asynchronous operation.

This property returns the object that is the last parameter of the method that initiates an asynchronous operation.

Notes to Implementers:

Implement this property to allow the caller of an asynchronous operation to obtain an application-defined object specified at the start of the operation.

Notes to Callers:

This object can be used to pass state information for the asynchronous operation to an AsyncCallback that you provide.

The following code example demonstrates how the AsyncState property is used to pass information to a callback method. The last parameter of the BeginInvoke method call is a format string, which the callback method uses to format an output message.

The example consists of two classes: the class that contains the method that is called asynchronously, and the class that contains the Main method that makes the call.

For more information about how this callback example works, and more examples of calling methods asynchronously by using delegates, see Calling Synchronous Methods Asynchronously.

using System;
using System.Threading; 

namespace Examples.AdvancedProgramming.AsynchronousOperations
    public class AsyncDemo 
        // The method to be executed asynchronously.
        public string TestMethod(int callDuration, out int threadId) 
            Console.WriteLine("Test method begins.");
            threadId = Thread.CurrentThread.ManagedThreadId;
            return String.Format("My call time was {0}.", callDuration.ToString());
    // The delegate must have the same signature as the method
    // it will call asynchronously.
    public delegate string AsyncMethodCaller(int callDuration, out int threadId);
using System;
using System.Threading;
using System.Runtime.Remoting.Messaging;

namespace Examples.AdvancedProgramming.AsynchronousOperations
    public class AsyncMain 
        static void Main() 
            // Create an instance of the test class.
            AsyncDemo ad = new AsyncDemo();

            // Create the delegate.
            AsyncMethodCaller caller = new AsyncMethodCaller(ad.TestMethod);

            // The threadId parameter of TestMethod is an out parameter, so
            // its input value is never used by TestMethod. Therefore, a dummy
            // variable can be passed to the BeginInvoke call. If the threadId
            // parameter were a ref parameter, it would have to be a class-
            // level field so that it could be passed to both BeginInvoke and 
            // EndInvoke.
            int dummy = 0;

            // Initiate the asynchronous call, passing three seconds (3000 ms)
            // for the callDuration parameter of TestMethod; a dummy variable 
            // for the out parameter (threadId); the callback delegate; and
            // state information that can be retrieved by the callback method.
            // In this case, the state information is a string that can be used
            // to format a console message.
            IAsyncResult result = caller.BeginInvoke(3000,
                out dummy, 
                new AsyncCallback(CallbackMethod),
                "The call executed on thread {0}, with return value \"{1}\".");

            Console.WriteLine("The main thread {0} continues to execute...", 

            // The callback is made on a ThreadPool thread. ThreadPool threads
            // are background threads, which do not keep the application running
            // if the main thread ends. Comment out the next line to demonstrate
            // this.

            Console.WriteLine("The main thread ends.");

        // The callback method must have the same signature as the
        // AsyncCallback delegate.
        static void CallbackMethod(IAsyncResult ar) 
            // Retrieve the delegate.
            AsyncResult result = (AsyncResult) ar;
            AsyncMethodCaller caller = (AsyncMethodCaller) result.AsyncDelegate;

            // Retrieve the format string that was passed as state 
            // information.
            string formatString = (string) ar.AsyncState;

            // Define a variable to receive the value of the out parameter.
            // If the parameter were ref rather than out then it would have to
            // be a class-level field so it could also be passed to BeginInvoke.
            int threadId = 0;

            // Call EndInvoke to retrieve the results.
            string returnValue = caller.EndInvoke(out threadId, ar);

            // Use the format string to format the output message.
            Console.WriteLine(formatString, threadId, returnValue);

/* This example produces output similar to the following:

The main thread 1 continues to execute...
Test method begins.
The call executed on thread 3, with return value "My call time was 3000.".
The main thread ends.

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top