Delen via


Synchrone methoden asynchroon aanroepen

Met .NET kunt u elke methode asynchroon aanroepen. Hiervoor definieert u een gemachtigde met dezelfde handtekening als de methode die u wilt aanroepen. De algemene taalruntime definieert BeginInvoke en EndInvoke methoden voor deze gemachtigde automatisch met de juiste handtekeningen.

Notitie

Asynchrone gemachtigdenaanroepen, met name de BeginInvoke en EndInvoke methoden, worden niet ondersteund in het .NET Compact Framework.

De BeginInvoke methode initieert de asynchrone aanroep. Het heeft dezelfde parameters als de methode die u asynchroon wilt uitvoeren, plus twee extra optionele parameters. De eerste parameter is een AsyncCallback gemachtigde die verwijst naar een methode die moet worden aangeroepen wanneer de asynchrone aanroep is voltooid. De tweede parameter is een door de gebruiker gedefinieerd object dat informatie doorgeeft aan de callback-methode. BeginInvoke retourneert onmiddellijk en wacht niet tot de asynchrone aanroep is voltooid. BeginInvoke retourneert een IAsyncResult, die kan worden gebruikt om de voortgang van de asynchrone aanroep te controleren.

De EndInvoke methode haalt de resultaten van de asynchrone aanroep op. Het kan op elk gewenst moment worden aangeroepen na BeginInvoke. Als de asynchrone aanroep niet is voltooid, blokkeert EndInvoke de aanroepende thread totdat deze is voltooid. De parameters van EndInvoke omvatten de out- en ref-parameters (<Out>ByRef en ByRef in Visual Basic) van de methode die u asynchroon wilt uitvoeren, plus de IAsyncResult die door BeginInvoke wordt geretourneerd.

Notitie

De functie IntelliSense in Visual Studio geeft de parameters van BeginInvoke en EndInvoke weer. Als u Visual Studio of een vergelijkbaar hulpprogramma niet gebruikt, of als u C# gebruikt met Visual Studio, raadpleegt u Asynchroon programmeermodel (APM) voor een beschrijving van de parameters die voor deze methoden zijn gedefinieerd.

De codevoorbeelden in dit onderwerp laten vier veelvoorkomende manieren zien om te gebruiken BeginInvoke en EndInvoke asynchrone aanroepen uit te voeren. Nadat u hebt gebeld BeginInvoke , kunt u het volgende doen:

  • Doe wat werk en roep EndInvoke vervolgens aan om te blokkeren totdat het gesprek is voltooid.

  • Haal een WaitHandle met behulp van de IAsyncResult.AsyncWaitHandle eigenschap op, gebruik de WaitOne methode om de uitvoering te blokkeren totdat het WaitHandle wordt gesignaleerd en roep vervolgens aan EndInvoke.

  • Controleer de door IAsyncResult geretourneerde BeginInvoke om te bepalen wanneer de asynchrone oproep is voltooid en roep vervolgens EndInvoke aan.

  • Geef een delegate door voor een callback-methode naar BeginInvoke. De methode wordt uitgevoerd op een ThreadPool thread wanneer de asynchrone aanroep is voltooid. De callback-methode roept EndInvokeaan.

Belangrijk

Ongeacht welke techniek u gebruikt, moet u altijd EndInvoke aanroepen om uw asynchrone aanroep te voltooien.

De testmethode en asynchrone gedelegeerde definiƫren

De codevoorbeelden die volgen laten verschillende manieren zien om dezelfde langlopende methode aan te roepen, TestMethodasynchroon. De TestMethod methode geeft een consolebericht weer om aan te geven dat het is begonnen met verwerken, een paar seconden slaapt en vervolgens eindigt. TestMethod heeft een out parameter om aan te tonen hoe dergelijke parameters worden toegevoegd aan de handtekeningen van BeginInvoke en EndInvoke. U kunt parameters op dezelfde manier verwerken ref .

In het volgende codevoorbeeld ziet u de definitie van TestMethod en de delegate AsyncMethodCaller die kan worden gebruikt om TestMethod asynchroon aan te roepen. Als u de codevoorbeelden wilt compileren, moet u de definities voor TestMethod en de AsyncMethodCaller gemachtigde opnemen.

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);
}
Imports System.Threading
Imports System.Runtime.InteropServices

Namespace Examples.AdvancedProgramming.AsynchronousOperations
    Public Class AsyncDemo
        ' The method to be executed asynchronously.
        Public Function TestMethod(ByVal callDuration As Integer, _
                <Out> ByRef threadId As Integer) As String
            Console.WriteLine("Test method begins.")
            Thread.Sleep(callDuration)
            threadId = Thread.CurrentThread.ManagedThreadId()
            return String.Format("My call time was {0}.", callDuration.ToString())
        End Function
    End Class

    ' The delegate must have the same signature as the method
    ' it will call asynchronously.
    Public Delegate Function AsyncMethodCaller(ByVal callDuration As Integer, _
        <Out> ByRef threadId As Integer) As String
End Namespace

Wachten op een asynchrone oproep met EndInvoke

De eenvoudigste manier om een methode asynchroon uit te voeren, is door de methode op de gemachtigde met BeginInvoke aan te roepen, wat werk te verrichten op de hoofdthread, en vervolgens de methode op de gemachtigde met EndInvoke aan te roepen. EndInvoke kan de aanroepende thread blokkeren omdat deze pas wordt geretourneerd als de asynchrone aanroep is voltooid. Dit is een goede techniek om te gebruiken met bestands- of netwerkbewerkingen.

Belangrijk

Omdat EndInvoke kan blokkeren, moet u het nooit aanroepen vanuit threads die de gebruikersinterface aansturen.

using System;
using System.Threading;

namespace Examples.AdvancedProgramming.AsynchronousOperations
{
    public class AsyncMain3
    {
        public static void Main()
        {
            // The asynchronous method puts the thread id here.
            int threadId;

            // Create an instance of the test class.
            AsyncDemo ad = new AsyncDemo();

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

            // Initiate the asynchronous call.
            IAsyncResult result = caller.BeginInvoke(3000,
                out threadId, null, null);

            Thread.Sleep(0);
            Console.WriteLine($"Main thread {Thread.CurrentThread.ManagedThreadId} does some work.");

            // Call EndInvoke to wait for the asynchronous call to complete,
            // and to retrieve the results.
            string returnValue = caller.EndInvoke(out threadId, result);

            Console.WriteLine("The call executed on thread {0}, with return value \"{1}\".",
                threadId, returnValue);
        }
    }
}

/* This example produces output similar to the following:

Main thread 1 does some work.
Test method begins.
The call executed on thread 3, with return value "My call time was 3000.".
 */
Imports System.Threading
Imports System.Runtime.InteropServices

Namespace Examples.AdvancedProgramming.AsynchronousOperations
    Public Class AsyncMain
        Shared Sub Main()
            ' The asynchronous method puts the thread id here.
            Dim threadId As Integer

            ' Create an instance of the test class.
            Dim ad As New AsyncDemo()

            ' Create the delegate.
            Dim caller As New AsyncMethodCaller(AddressOf ad.TestMethod)

            ' Initiate the asynchronous call.
            Dim result As IAsyncResult = caller.BeginInvoke(3000, _
                threadId, Nothing, Nothing)

            Thread.Sleep(0)
            Console.WriteLine("Main thread {0} does some work.", _
                 Thread.CurrentThread.ManagedThreadId)

            ' Call EndInvoke to Wait for the asynchronous call to complete,
            ' and to retrieve the results.
            Dim returnValue As String = caller.EndInvoke(threadId, result)

            Console.WriteLine("The call executed on thread {0}, with return value ""{1}"".", _
                threadId, returnValue)
        End Sub
    End Class

End Namespace

'This example produces output similar to the following:
'
'Main thread 1 does some work.
'Test method begins.
'The call executed on thread 3, with return value "My call time was 3000.".

Wachten op een asynchrone oproep met WaitHandle

U kunt een WaitHandle verkrijgen door de eigenschap AsyncWaitHandle te gebruiken van de door IAsyncResult geretourneerde BeginInvoke. De WaitHandle melding wordt weergegeven wanneer de asynchrone aanroep is voltooid en u kunt erop wachten door de WaitOne methode aan te roepen.

Als u een WaitHandle gebruikt, kunt u extra verwerking uitvoeren voor of nadat de asynchrone aanroep is voltooid, maar voordat u EndInvoke aanroept om de resultaten op te halen.

Notitie

De wachtgreep wordt niet automatisch gesloten wanneer u EndInvoke aanroept. Als u alle verwijzingen naar het wachtobject vrijgeeft, worden systeembronnen vrijgemaakt wanneer garbage collection het wachtobject opnieuw ophaalt. Als u de systeembronnen wilt vrijmaken zodra u klaar bent met het wachtobject, kunt u dit vrijgeven door de WaitHandle.Close-methode aan te roepen. Garbagecollection werkt efficiƫnter wanneer wegwerpobjecten expliciet worden verwijderd.

using System;
using System.Threading;

namespace Examples.AdvancedProgramming.AsynchronousOperations
{
    public class AsyncMain2
    {
        static void Main()
        {
            // The asynchronous method puts the thread id here.
            int threadId;

            // Create an instance of the test class.
            AsyncDemo ad = new AsyncDemo();

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

            // Initiate the asynchronous call.
            IAsyncResult result = caller.BeginInvoke(3000,
                out threadId, null, null);

            Thread.Sleep(0);
            Console.WriteLine($"Main thread {Thread.CurrentThread.ManagedThreadId} does some work.");

            // Wait for the WaitHandle to become signaled.
            result.AsyncWaitHandle.WaitOne();

            // Perform additional processing here.
            // Call EndInvoke to retrieve the results.
            string returnValue = caller.EndInvoke(out threadId, result);

            // Close the wait handle.
            result.AsyncWaitHandle.Close();

            Console.WriteLine("The call executed on thread {0}, with return value \"{1}\".",
                threadId, returnValue);
        }
    }
}

/* This example produces output similar to the following:

Main thread 1 does some work.
Test method begins.
The call executed on thread 3, with return value "My call time was 3000.".
 */
Imports System.Threading
Imports System.Runtime.InteropServices

Namespace Examples.AdvancedProgramming.AsynchronousOperations

    Public Class AsyncMain
        Shared Sub Main()
            ' The asynchronous method puts the thread id here.
            Dim threadId As Integer

            ' Create an instance of the test class.
            Dim ad As New AsyncDemo()

            ' Create the delegate.
            Dim caller As New AsyncMethodCaller(AddressOf ad.TestMethod)

            ' Initiate the asynchronous call.
            Dim result As IAsyncResult = caller.BeginInvoke(3000, _
                threadId, Nothing, Nothing)

            Thread.Sleep(0)
            Console.WriteLine("Main thread {0} does some work.", _
                Thread.CurrentThread.ManagedThreadId)
            ' Perform additional processing here and then
            ' wait for the WaitHandle to be signaled.
            result.AsyncWaitHandle.WaitOne()

            ' Call EndInvoke to retrieve the results.
            Dim returnValue As String = caller.EndInvoke(threadId, result)

            ' Close the wait handle.
            result.AsyncWaitHandle.Close()

            Console.WriteLine("The call executed on thread {0}, with return value ""{1}"".", _
                threadId, returnValue)
        End Sub
    End Class
End Namespace

'This example produces output similar to the following:
'
'Main thread 1 does some work.
'Test method begins.
'The call executed on thread 3, with return value "My call time was 3000.".

Polling voor de voltooiing van een asynchrone aanroep

U kunt de IsCompleted eigenschap gebruiken van het door IAsyncResult geretourneerde BeginInvoke om vast te stellen wanneer de asynchrone aanroep is voltooid. U kunt dit doen wanneer u de asynchrone aanroep uitvoert vanuit een thread die de gebruikersinterface services. Door te polleren voor voltooiing kan de aanroepende thread doorgaan met uitvoeren terwijl de asynchrone aanroep op een ThreadPool thread wordt uitgevoerd.

using System;
using System.Threading;

namespace Examples.AdvancedProgramming.AsynchronousOperations
{
    public class AsyncMain
    {
        static void Main() {
            // The asynchronous method puts the thread id here.
            int threadId;

            // Create an instance of the test class.
            AsyncDemo ad = new AsyncDemo();

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

            // Initiate the asynchronous call.
            IAsyncResult result = caller.BeginInvoke(3000,
                out threadId, null, null);

            // Poll while simulating work.
            while(result.IsCompleted == false) {
                Thread.Sleep(250);
                Console.Write(".");
            }

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

            Console.WriteLine("\nThe call executed on thread {0}, with return value \"{1}\".",
                threadId, returnValue);
        }
    }
}

/* This example produces output similar to the following:

Test method begins.
.............
The call executed on thread 3, with return value "My call time was 3000.".
 */
Imports System.Threading
Imports System.Runtime.InteropServices

Namespace Examples.AdvancedProgramming.AsynchronousOperations

    Public Class AsyncMain
        Shared Sub Main()
            ' The asynchronous method puts the thread id here.
            Dim threadId As Integer

            ' Create an instance of the test class.
            Dim ad As New AsyncDemo()

            ' Create the delegate.
            Dim caller As New AsyncMethodCaller(AddressOf ad.TestMethod)

            ' Initiate the asynchronous call.
            Dim result As IAsyncResult = caller.BeginInvoke(3000, _
                threadId, Nothing, Nothing)

            ' Poll while simulating work.
            While result.IsCompleted = False
                Thread.Sleep(250)
                Console.Write(".")
            End While

            ' Call EndInvoke to retrieve the results.
            Dim returnValue As String = caller.EndInvoke(threadId, result)

            Console.WriteLine(vbCrLf & _
                "The call executed on thread {0}, with return value ""{1}"".", _
                threadId, returnValue)
        End Sub
    End Class
End Namespace

' This example produces output similar to the following:
'
'Test method begins.
'.............
'The call executed on thread 3, with return value "My call time was 3000.".

Een callbackmethode uitvoeren wanneer een asynchrone aanroep is voltooid

Als de thread waarmee de asynchrone aanroep wordt gestart, niet de thread hoeft te zijn die de resultaten verwerkt, kunt u een callback-methode uitvoeren wanneer de aanroep is voltooid. De callback-methode wordt uitgevoerd op een ThreadPool thread.

Als u een callback-methode wilt gebruiken, moet u een BeginInvoke gemachtigde doorgeven AsyncCallback die de callback-methode vertegenwoordigt. U kunt ook een object doorgeven dat informatie bevat die moet worden gebruikt door de callback-methode. In de callback-methode kunt u de IAsyncResult, wat de enige parameter van de callback-methode is, casten naar een AsyncResult object. Vervolgens kunt u de AsyncResult.AsyncDelegate eigenschap gebruiken om de gemachtigde op te halen die is gebruikt om de oproep te starten, zodat u kunt bellen EndInvoke.

Opmerkingen in het voorbeeld:

  • De threadId-parameter van TestMethod is een out-parameter ([<Out>ByRef in Visual Basic), dus de invoerwaarde wordt nooit gebruikt door TestMethod. Er wordt een dummyvariabele doorgegeven aan de BeginInvoke aanroep. Als de threadId parameter een ref parameter was (ByRef in Visual Basic), zou de variabele een veld op klasseniveau moeten zijn, zodat het kan worden doorgegeven aan zowel BeginInvoke als EndInvoke.

  • De statusinformatie die naar BeginInvoke wordt doorgegeven, is een formaatreeks die door de callback-methode wordt gebruikt om een uitvoerbericht op te maken. Omdat deze als type Objectwordt doorgegeven, moet de statusinformatie worden omgezet in het juiste type voordat deze kan worden gebruikt.

  • De callback wordt uitgevoerd op een ThreadPool thread. ThreadPool threads zijn achtergrondthreads, die de toepassing niet actief houden als de hoofdthread eindigt, dus de hoofdthread van het voorbeeld moet lang genoeg slapen om de callback te voltooien.

using System;
using System.Threading;
using System.Runtime.Remoting.Messaging;

namespace Examples.AdvancedProgramming.AsynchronousOperations
{
    public class AsyncMain4
    {
        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 {Thread.CurrentThread.ManagedThreadId} 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.
            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.
 */
Imports System.Threading
Imports System.Runtime.Remoting.Messaging

Namespace Examples.AdvancedProgramming.AsynchronousOperations

    Public Class AsyncMain

        Shared Sub Main()

            ' Create an instance of the test class.
            Dim ad As New AsyncDemo()

            ' Create the delegate.
            Dim caller As New AsyncMethodCaller(AddressOf 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 ByRef parameter, it would have to be a class-
            ' level field so that it could be passed to both BeginInvoke and 
            ' EndInvoke.
            Dim dummy As Integer = 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.
            Dim result As IAsyncResult = caller.BeginInvoke(3000, _
                dummy, _
                AddressOf 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.")
        End Sub

        ' The callback method must have the same signature as the
        ' AsyncCallback delegate.
        Shared Sub CallbackMethod(ByVal ar As IAsyncResult)
            ' Retrieve the delegate.
            Dim result As AsyncResult = CType(ar, AsyncResult)
            Dim caller As AsyncMethodCaller = CType(result.AsyncDelegate, AsyncMethodCaller)

            ' Retrieve the format string that was passed as state 
            ' information.
            Dim formatString As String = CType(ar.AsyncState, String)

            ' Define a variable to receive the value of the <Out> parameter.
            ' If the parameter were ByRef rather than <Out> then it would have to
            ' be a class-level field so it could also be passed to BeginInvoke.
            Dim threadId As Integer = 0

            ' Call EndInvoke to retrieve the results.
            Dim returnValue As String = caller.EndInvoke(threadId, ar)

            ' Use the format string to format the output message.
            Console.WriteLine(formatString, threadId, returnValue)
        End Sub
    End Class
End Namespace

' 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.

Zie ook