Auf Englisch lesen

Freigeben über


IAsyncResult.AsyncState Eigenschaft

Definition

Ruft ein benutzerdefiniertes Objekt ab, das einen asynchronen Vorgang qualifiziert oder Informationen darüber enthält.

C#
public object AsyncState { get; }
C#
public object? AsyncState { get; }

Eigenschaftswert

Object

Ein benutzerdefiniertes Objekt, das einen asynchronen Vorgang qualifiziert oder Informationen darüber enthält.

Beispiele

Im folgenden Codebeispiel wird veranschaulicht, wie die AsyncState Eigenschaft verwendet wird, um Informationen an eine Rückrufmethode zu übergeben. Der letzte Parameter des BeginInvoke Methodenaufrufs ist eine Formatzeichenfolge, mit der die Rückrufmethode verwendet wird, um eine Ausgabemeldung zu formatieren.

Das Beispiel besteht aus zwei Klassen: die Klasse, die die Methode enthält, die asynchron aufgerufen wird, und die Klasse, die die Main Methode enthält, die den Aufruf vornimmt.

Weitere Informationen dazu, wie dieses Rückrufbeispiel funktioniert, und weitere Beispiele für Aufrufmethoden asynchron mithilfe von Stellvertretungen finden Sie unter Aufrufen synchroner Methoden asynchron.

C#
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.");
            Thread.Sleep(callDuration);
            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);
}
C#
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...",
                Thread.CurrentThread.ManagedThreadId);

            // 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.
            Thread.Sleep(4000);

            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.
 */

Hinweise

Diese Eigenschaft gibt das Objekt zurück, das der letzte Parameter der Methode ist, die einen asynchronen Vorgang initiiert.

Hinweise für Ausführende

Implementieren Sie diese Eigenschaft, damit der Aufrufer eines asynchronen Vorgangs ein anwendungsdefiniertes Objekt abrufen kann, das am Anfang des Vorgangs angegeben ist.

Hinweise für Aufrufer

Dieses Objekt kann verwendet werden, um Statusinformationen für den asynchronen Vorgang an einen AsyncCallback von Ihnen bereitgestellten Vorgang zu übergeben.

Gilt für

Produkt Versionen
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Siehe auch