TPL und herkömmliche asynchrone .NET Framework-ProgrammierungTPL and Traditional .NET Framework Asynchronous Programming

.NET Framework stellt die folgenden zwei Standardmuster zum Ausführen von E/A-gebundenen und rechnergebundenen asynchronen Vorgängen bereit:The .NET Framework provides the following two standard patterns for performing I/O-bound and compute-bound asynchronous operations:

  • Das asynchrone Programmiermodell (APM), in dem asynchrone Vorgänge durch ein Paar von Begin/End-Methoden dargestellt werden, z. B. FileStream.BeginRead und Stream.EndRead.Asynchronous Programming Model (APM), in which asynchronous operations are represented by a pair of Begin/End methods such as FileStream.BeginRead and Stream.EndRead.

  • Das ereignisbasierte asynchrone Muster (EAP), in dem asynchrone Vorgänge durch ein Methoden-/Ereignispaar mit den Namen OperationNameAsync und OperationNameCompleted dargestellt werden, z. B. WebClient.DownloadStringAsync und WebClient.DownloadStringCompleted.Event-based asynchronous pattern (EAP), in which asynchronous operations are represented by a method/event pair that are named OperationNameAsync and OperationNameCompleted, for example, WebClient.DownloadStringAsync and WebClient.DownloadStringCompleted. (EAP wurde in .NET Framework 2.0 eingeführt.)(EAP was introduced in the .NET Framework version 2.0.)

Die Task Parallel Library (TPL) kann mit beiden asynchronen Mustern auf unterschiedlichste Weise verwendet werden.The Task Parallel Library (TPL) can be used in various ways in conjunction with either of the asynchronous patterns. Sie können sowohl APM-, als auch EAP-Vorgänge als Aufgaben für Bibliotheksconsumer verfügbar machen, oder Sie machen die APM-Muster verfügbar, implementieren diese aber intern mithilfe von Task-Objekten.You can expose both APM and EAP operations as Tasks to library consumers, or you can expose the APM patterns but use Task objects to implement them internally. In beiden Fällen können Sie den Code mithilfe von Aufgabenobjekten vereinfachen und profitieren von der folgenden nützlichen Funktionalität:In both scenarios, by using Task objects, you can simplify the code and take advantage of the following useful functionality:

  • Sie können jederzeit nach dem Start der Aufgabe Rückrufe in der Form von Aufgabenfortsetzungen registrieren.Register callbacks, in the form of task continuations, at any time after the task has started.

  • Sie können mehrere Vorgänge, die als Reaktion auf eine Begin_-Methode ausgeführt werden, mit den Methoden ContinueWhenAll und ContinueWhenAny, WaitAll oder WaitAny koordinieren.Coordinate multiple operations that execute in response to a Begin_ method, by using the ContinueWhenAll and ContinueWhenAny methods, or the WaitAll method or the WaitAny method.

  • Sie können asynchrone E/A-gebundenen und rechnergebundene Vorgänge im gleichen Task-Objekt kapseln.Encapsulate asynchronous I/O-bound and compute-bound operations in the same Task object.

  • Sie können den Status des Task-Objekts überwachen.Monitor the status of the Task object.

  • Sie können den Status eines Vorgangs mittels TaskCompletionSource<TResult> an ein Task-Objekt marshallen.Marshal the status of an operation to a Task object by using TaskCompletionSource<TResult>.

Einschließen von APM-Vorgängen in eine AufgabeWrapping APM Operations in a Task

Sowohl die System.Threading.Tasks.TaskFactory-Klasse als auch die System.Threading.Tasks.TaskFactory<TResult>-Klasse stellen mehrere Überladungen der TaskFactory.FromAsync-Methode und der TaskFactory<TResult>.FromAsync-Methode bereit, mit denen Sie ein APM Begin/End-Methodenpaar in einer Task-oder Task<TResult>-Instanz kapseln können.Both the System.Threading.Tasks.TaskFactory and System.Threading.Tasks.TaskFactory<TResult> classes provide several overloads of the TaskFactory.FromAsync and TaskFactory<TResult>.FromAsync methods that let you encapsulate an APM Begin/End method pair in one Task or Task<TResult> instance. Die verschiedenen Überladungen unterstützen beliebige Begin/End-Methodenpaare mit null bis drei Eingabeparametern.The various overloads accommodate any Begin/End method pair that have from zero to three input parameters.

Verwenden Sie für Paare mit End-Methoden, die einen Wert (Function in Visual Basic) zurückgeben, die Methoden in TaskFactory<TResult>, die ein Task<TResult> erstellen.For pairs that have End methods that return a value (Function in Visual Basic), use the methods in TaskFactory<TResult> that create a Task<TResult>. Verwenden Sie für End-Methoden, die "void" (Sub in Visual Basic) zurückgeben, die Methoden in TaskFactory, die ein Task erstellen.For End methods that return void (Sub in Visual Basic), use the methods in TaskFactory that create a Task.

In den wenigen Fälle, in denen die Begin-Methode über mehr als drei Parameter verfügt oder ref-Parameter bzw. out-Parameter enthält, werden zusätzliche FromAsync-Überladungen bereitgestellt, die nur die End-Methode kapseln.For those few cases in which the Begin method has more than three parameters or contains ref or out parameters, additional FromAsync overloads that encapsulate only the End method are provided.

Im folgenden Beispiel wird die Signatur für die FromAsync-Überladung dargestellt, die den Methoden FileStream.BeginRead und FileStream.EndRead entspricht.The following example shows the signature for the FromAsync overload that matches the FileStream.BeginRead and FileStream.EndRead methods. Diese Überladung lässt drei Eingabeparameter zu.This overload takes three input parameters, as follows.

public Task<TResult> FromAsync<TArg1, TArg2, TArg3>(
    Func<TArg1, TArg2, TArg3, AsyncCallback, object, IAsyncResult> beginMethod, //BeginRead
     Func<IAsyncResult, TResult> endMethod, //EndRead
     TArg1 arg1, // the byte[] buffer
     TArg2 arg2, // the offset in arg1 at which to start writing data
     TArg3 arg3, // the maximum number of bytes to read
     object state // optional state information
    ) 
Public Function FromAsync(Of TArg1, TArg2, TArg3)(
                ByVal beginMethod As Func(Of TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult),
                ByVal endMethod As Func(Of IAsyncResult, TResult),
                ByVal dataBuffer As TArg1,
                ByVal byteOffsetToStartAt As TArg2,
                ByVal maxBytesToRead As TArg3,
                ByVal stateInfo As Object)

Der erste Parameter ist ein Func<T1,T2,T3,T4,T5,TResult>-Delegat, der mit der Signatur der FileStream.BeginRead-Methode übereinstimmt.The first parameter is a Func<T1,T2,T3,T4,T5,TResult> delegate that matches the signature of the FileStream.BeginRead method. Der zweite Parameter ist ein Func<T,TResult>-Delegat, der IAsyncResult als Eingabe verwendet und TResult zurückgibt.The second parameter is a Func<T,TResult> delegate that takes an IAsyncResult and returns a TResult. Da EndRead eine ganze Zahl zurückgibt, leitet der Compiler den Typ von TResult als Int32 und den Typ der Aufgabe als Task ab.Because EndRead returns an integer, the compiler infers the type of TResult as Int32 and the type of the task as Task. Die letzten vier Parameter sind mit denen in der FileStream.BeginRead-Methode identisch:The last four parameters are identical to those in the FileStream.BeginRead method:

  • Der Puffer, in dem die Dateidaten gespeichert werden sollen.The buffer in which to store the file data.

  • Der Offset im Puffer, ab dem die Daten geschrieben werden sollen.The offset in the buffer at which to begin writing data.

  • Die maximale Datenmenge, die aus der Datei gelesen werden soll.The maximum amount of data to read from the file.

  • Ein optionales Objekt, in dem benutzerdefinierte Zustandsdaten speichert werden, die an den Rückruf übergeben werden sollen.An optional object that stores user-defined state data to pass to the callback.

Verwenden von ContinueWith für die RückruffunktionalitätUsing ContinueWith for the Callback Functionality

Wenn Sie nicht nur die Anzahl der Byte in der Datei ermitteln möchten, sondern Zugriff auf die Daten in der Datei benötigen, reicht die FromAsync-Methode nicht aus.If you require access to the data in the file, as opposed to just the number of bytes, the FromAsync method is not sufficient. Verwenden Sie stattdessen Task, deren Result-Eigenschaft die Dateidaten enthält.Instead, use Task, whose Result property contains the file data. Fügen Sie hierzu eine Fortsetzung zu der ursprünglichen Aufgabe hinzu.You can do this by adding a continuation to the original task. Die Fortsetzung führt die Aufgaben aus, die normalerweise vom AsyncCallback-Delegaten ausgeführt würde.The continuation performs the work that would typically be performed by the AsyncCallback delegate. Sie wird aufgerufen, wenn der Vorgänger abgeschlossen und der Datenpuffer gefüllt wurde.It is invoked when the antecedent completes, and the data buffer has been filled. (Das FileStream-Objekt sollte vor der Rückkehr geschlossen werden.)(The FileStream object should be closed before returning.)

Im folgenden Beispiel wird gezeigt, wie Sie Task zurückgeben, die das BeginRead/EndRead-Paar der FileStream-Klasse kapselt.The following example shows how to return a Task that encapsulates the BeginRead/EndRead pair of the FileStream class.

const int MAX_FILE_SIZE = 14000000;
public static Task<string> GetFileStringAsync(string path)
{
    FileInfo fi = new FileInfo(path);
    byte[] data = null;
    data = new byte[fi.Length];

    FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, data.Length, true);

    //Task<int> returns the number of bytes read
    Task<int> task = Task<int>.Factory.FromAsync(
            fs.BeginRead, fs.EndRead, data, 0, data.Length, null);

    // It is possible to do other work here while waiting
    // for the antecedent task to complete.
    // ...

    // Add the continuation, which returns a Task<string>. 
    return task.ContinueWith((antecedent) =>
    {
        fs.Close();

        // Result = "number of bytes read" (if we need it.)
        if (antecedent.Result < 100)
        {
            return "Data is too small to bother with.";
        }
        else
        {
            // If we did not receive the entire file, the end of the
            // data buffer will contain garbage.
            if (antecedent.Result < data.Length)
                Array.Resize(ref data, antecedent.Result);

            // Will be returned in the Result property of the Task<string>
            // at some future point after the asynchronous file I/O operation completes.
            return new UTF8Encoding().GetString(data);
        }
    });
}
Const MAX_FILE_SIZE As Integer = 14000000
Shared Function GetFileStringAsync(ByVal path As String) As Task(Of String)
    Dim fi As New FileInfo(path)
    Dim data(fi.Length) As Byte

    Dim fs As FileStream = New FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, data.Length, True)

    ' Task(Of Integer) returns the number of bytes read
    Dim myTask As Task(Of Integer) = Task(Of Integer).Factory.FromAsync(
        AddressOf fs.BeginRead, AddressOf fs.EndRead, data, 0, data.Length, Nothing)

    ' It is possible to do other work here while waiting
    ' for the antecedent task to complete.
    ' ...

    ' Add the continuation, which returns a Task<string>. 
    Return myTask.ContinueWith(Function(antecedent)
                                   fs.Close()
                                   If (antecedent.Result < 100) Then
                                       Return "Data is too small to bother with."
                                   End If
                                   ' If we did not receive the entire file, the end of the
                                   ' data buffer will contain garbage.
                                   If (antecedent.Result < data.Length) Then
                                       Array.Resize(data, antecedent.Result)
                                   End If

                                   ' Will be returned in the Result property of the Task<string>
                                   ' at some future point after the asynchronous file I/O operation completes.
                                   Return New UTF8Encoding().GetString(data)
                               End Function)

End Function

Die Methode kann anschließend wie folgt aufgerufen werden.The method can then be called, as follows.


 Task<string> t = GetFileStringAsync(path);          

 // Do some other work:
 // ...

 try
 {
      Console.WriteLine(t.Result.Substring(0, 500));
 }
 catch (AggregateException ae)
 {
     Console.WriteLine(ae.InnerException.Message);
 }            
Dim myTask As Task(Of String) = GetFileStringAsync(path)

' Do some other work
' ...

Try
    Console.WriteLine(myTask.Result.Substring(0, 500))
Catch ex As AggregateException
    Console.WriteLine(ex.InnerException.Message)
End Try

Bereitstellen von benutzerdefinierten ZustandsdatenProviding Custom State Data

Wenn in typischen IAsyncResult-Vorgängen der AsyncCallback-Delegat bestimmte benutzerdefinierte Zustandsdaten erfordert, müssen Sie diese mit dem letzten Parameter in der Begin-Methode übergeben, damit die Daten in das IAsyncResult-Objekt eingebunden werden können, das schließlich an die Rückrufmethode übergeben wird.In typical IAsyncResult operations, if your AsyncCallback delegate requires some custom state data, you have to pass it in through the last parameter in the Begin method, so that the data can be packaged into the IAsyncResult object that is eventually passed to the callback method. Dies ist in der Regel nicht erforderlich, wenn die FromAsync-Methoden verwendet werden.This is typically not required when the FromAsync methods are used. Wenn die benutzerdefinierten Daten in der Fortsetzung bekannt sind, können diese direkt im Fortsetzungsdelegaten erfasst werden.If the custom data is known to the continuation, then it can be captured directly in the continuation delegate. Das folgende Beispiel ähnelt dem vorherigen Beispiel, anstatt die Result-Eigenschaft des Vorgängers zu analysieren, ermittelt die Fortsetzung jedoch die benutzerdefinierten Zustandsdaten, auf die der Benutzerdelegat der Fortsetzung direkt zugreifen kann.The following example resembles the previous example, but instead of examining the Result property of the antecedent, the continuation examines the custom state data that is directly accessible to the user delegate of the continuation.

public Task<string> GetFileStringAsync2(string path)
{             
    FileInfo fi = new FileInfo(path);
    byte[] data = new byte[fi.Length];                       
    MyCustomState state = GetCustomState();
    FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, data.Length, true);
    // We still pass null for the last parameter because
    // the state variable is visible to the continuation delegate.
    Task<int> task = Task<int>.Factory.FromAsync(
            fs.BeginRead, fs.EndRead, data, 0, data.Length, null);

    return task.ContinueWith((antecedent) =>
    {
        // It is safe to close the filestream now.
        fs.Close();

        // Capture custom state data directly in the user delegate.
        // No need to pass it through the FromAsync method.
        if (state.StateData.Contains("New York, New York"))
        {
            return "Start spreading the news!";
        }
        else
        {
            // If we did not receive the entire file, the end of the
            // data buffer will contain garbage.
            if (antecedent.Result < data.Length)
                Array.Resize(ref data, antecedent.Result);

            // Will be returned in the Result property of the Task<string>
            // at some future point after the asynchronous file I/O operation completes.
            return new UTF8Encoding().GetString(data);
        }
    });
    
}
Public Function GetFileStringAsync2(ByVal path As String) As Task(Of String)
    Dim fi = New FileInfo(path)
    Dim data(fi.Length) As Byte
    Dim state As New MyCustomState()

    Dim fs As New FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, data.Length, True)
    ' We still pass null for the last parameter because
    ' the state variable is visible to the continuation delegate.
    Dim myTask As Task(Of Integer) = Task(Of Integer).Factory.FromAsync(
            AddressOf fs.BeginRead, AddressOf fs.EndRead, data, 0, data.Length, Nothing)

    Return myTask.ContinueWith(Function(antecedent)
                                   fs.Close()
                                   ' Capture custom state data directly in the user delegate.
                                   ' No need to pass it through the FromAsync method.
                                   If (state.StateData.Contains("New York, New York")) Then
                                       Return "Start spreading the news!"
                                   End If

                                   ' If we did not receive the entire file, the end of the
                                   ' data buffer will contain garbage.
                                   If (antecedent.Result < data.Length) Then
                                       Array.Resize(data, antecedent.Result)
                                   End If
                                   '/ Will be returned in the Result property of the Task<string>
                                   '/ at some future point after the asynchronous file I/O operation completes.
                                   Return New UTF8Encoding().GetString(data)
                               End Function)

End Function

Synchronisieren mehrerer FromAsync-AufgabenSynchronizing Multiple FromAsync Tasks

Die statischen Methoden ContinueWhenAll und ContinueWhenAny bieten bei Verwendung mit den FromAsync-Methoden eine höhere Flexibilität.The static ContinueWhenAll and ContinueWhenAny methods provide added flexibility when used in conjunction with the FromAsync methods. Im folgenden Beispiel wird gezeigt, wie Sie mehrere asynchrone E/A-Vorgänge initiieren und anschließend auf den Abschluss all dieser Vorgänge warten, bevor die Fortsetzung ausgeführt wird.The following example shows how to initiate multiple asynchronous I/O operations, and then wait for all of them to complete before you execute the continuation.

public Task<string> GetMultiFileData(string[] filesToRead)
{
    FileStream fs;
    Task<string>[] tasks = new Task<string>[filesToRead.Length];
    byte[] fileData = null;
    for (int i = 0; i < filesToRead.Length; i++)
    {
        fileData = new byte[0x1000];
        fs = new FileStream(filesToRead[i], FileMode.Open, FileAccess.Read, FileShare.Read, fileData.Length, true);

        // By adding the continuation here, the 
        // Result of each task will be a string.
        tasks[i] = Task<int>.Factory.FromAsync(
                 fs.BeginRead, fs.EndRead, fileData, 0, fileData.Length, null)
                 .ContinueWith((antecedent) =>
                     {
                         fs.Close();

                         // If we did not receive the entire file, the end of the
                         // data buffer will contain garbage.
                         if (antecedent.Result < fileData.Length)
                             Array.Resize(ref fileData, antecedent.Result);

                         // Will be returned in the Result property of the Task<string>
                         // at some future point after the asynchronous file I/O operation completes.
                         return new UTF8Encoding().GetString(fileData);
                     });
    }

    // Wait for all tasks to complete. 
    return Task<string>.Factory.ContinueWhenAll(tasks, (data) =>
    {
        // Propagate all exceptions and mark all faulted tasks as observed.
        Task.WaitAll(data);

        // Combine the results from all tasks.
        StringBuilder sb = new StringBuilder();
        foreach (var t in data)
        {
            sb.Append(t.Result);
        }
        // Final result to be returned eventually on the calling thread.
        return sb.ToString();
    });

}
Public Function GetMultiFileData(ByVal filesToRead As String()) As Task(Of String)
    Dim fs As FileStream
    Dim tasks(filesToRead.Length) As Task(Of String)
    Dim fileData() As Byte = Nothing
    For i As Integer = 0 To filesToRead.Length
        fileData(&H1000) = New Byte()
        fs = New FileStream(filesToRead(i), FileMode.Open, FileAccess.Read, FileShare.Read, fileData.Length, True)

        ' By adding the continuation here, the 
        ' Result of each task will be a string.
        tasks(i) = Task(Of Integer).Factory.FromAsync(AddressOf fs.BeginRead,
                                                      AddressOf fs.EndRead,
                                                      fileData,
                                                      0,
                                                      fileData.Length,
                                                      Nothing).
                                                  ContinueWith(Function(antecedent)
                                                                   fs.Close()
                                                                   'If we did not receive the entire file, the end of the
                                                                   ' data buffer will contain garbage.
                                                                   If (antecedent.Result < fileData.Length) Then
                                                                       ReDim Preserve fileData(antecedent.Result)
                                                                   End If

                                                                   'Will be returned in the Result property of the Task<string>
                                                                   ' at some future point after the asynchronous file I/O operation completes.
                                                                   Return New UTF8Encoding().GetString(fileData)
                                                               End Function)
    Next

    Return Task(Of String).Factory.ContinueWhenAll(tasks, Function(data)

                                                              ' Propagate all exceptions and mark all faulted tasks as observed.
                                                              Task.WaitAll(data)

                                                              ' Combine the results from all tasks.
                                                              Dim sb As New StringBuilder()
                                                              For Each t As Task(Of String) In data
                                                                  sb.Append(t.Result)
                                                              Next
                                                              ' Final result to be returned eventually on the calling thread.
                                                              Return sb.ToString()
                                                          End Function)
End Function

FromAsync-Aufgaben für die End-MethodeFromAsync Tasks For Only the End Method

In den wenigen Fällen, in denen die Begin-Methode mehr als drei Eingabeparameter erfordert oder über den ref-Parameter bzw. out-Parameter verfügt, können Sie die FromAsync-Überladungen verwenden, z. B. TaskFactory<TResult>.FromAsync(IAsyncResult, Func<IAsyncResult,TResult>), die nur die End-Methode darstellen.For those few cases in which the Begin method requires more than three input parameters, or has ref or out parameters, you can use the FromAsync overloads, for example, TaskFactory<TResult>.FromAsync(IAsyncResult, Func<IAsyncResult,TResult>), that represent only the End method. Diese Methoden können zudem in jedem Szenario verwendet werden, in dem ein IAsyncResult übergeben wurde, das in einer Aufgabe gekapselt werden soll.These methods can also be used in any scenario in which you are passed an IAsyncResult and want to encapsulate it in a Task.

static Task<String> ReturnTaskFromAsyncResult()
{
    IAsyncResult ar = DoSomethingAsynchronously();
    Task<String> t = Task<string>.Factory.FromAsync(ar, _ =>
        {
            return (string)ar.AsyncState;
        });

    return t;
}
Shared Function ReturnTaskFromAsyncResult() As Task(Of String)
    Dim ar As IAsyncResult = DoSomethingAsynchronously()
    Dim t As Task(Of String) = Task(Of String).Factory.FromAsync(ar, Function(res) CStr(res.AsyncState))
    Return t
End Function

Starten und Abbrechen von FromAsync-AufgabenStarting and Canceling FromAsync Tasks

Die von einer FromAsync-Methode zurückgegebene Aufgabe hat den Status "WaitingForActivation" und wird vom System gestartet, nachdem die Aufgabe erstellt wurde.The task returned by a FromAsync method has a status of WaitingForActivation and will be started by the system at some point after the task is created. Wenn Sie versuchen, eine solche Aufgabe zu starten, wird eine Ausnahme ausgelöst.If you attempt to call Start on such a task, an exception will be raised.

Sie können eine FromAsync-Aufgabe nicht abbrechen, da die zugrunde liegenden .NET Framework-APIs keinen Abbruch von laufenden Datei- oder Netzwerk-E/A unterstützen.You cannot cancel a FromAsync task, because the underlying .NET Framework APIs currently do not support in-progress cancellation of file or network I/O. Sie können Abbruchfunktionalität zu einer Methode hinzufügen, die einen FromAsync-Aufruf kapselt, Sie können jedoch nur vor dem Aufruf oder nach dem Abschluss von FromAsync (z. B. in einer Fortsetzungsaufgabe) auf den Abbruch reagieren.You can add cancellation functionality to a method that encapsulates a FromAsync call, but you can only respond to the cancellation before FromAsync is called or after it completed (for example, in a continuation task).

Einige Klassen mit EAP-Unterstützung, z. B. WebClient, unterstützen einen Abbruch. Diese systemeigene Abbruchfunktionalität können Sie mithilfe von Abbruchtokens integrieren.Some classes that support EAP, for example, WebClient, do support cancellation, and you can integrate that native cancellation functionality by using cancellation tokens.

Bereitstellen komplexer EAP-Vorgänge als AufgabenExposing Complex EAP Operations As Tasks

Der TPL stellt keine Methoden bereit, die speziell zum Kapseln ereignisbasierter asynchroner Vorgänge geeignet sind, wie dies bei der FromAsync-Methodenfamilie und dem IAsyncResult-Muster der Fall ist.The TPL does not provide any methods that are specifically designed to encapsulate an event-based asynchronous operation in the same way that the FromAsync family of methods wrap the IAsyncResult pattern. Die TPL bietet jedoch die System.Threading.Tasks.TaskCompletionSource<TResult>-Klasse, mit der ein beliebiger Satz von Vorgängen als Task<TResult> dargestellt werden kann.However, the TPL does provide the System.Threading.Tasks.TaskCompletionSource<TResult> class, which can be used to represent any arbitrary set of operations as a Task<TResult>. Die Vorgänge können sowohl synchron als auch asynchron sein und sind entweder E/A-gebunden und/oder rechnergebunden.The operations may be synchronous or asynchronous, and may be I/O bound or compute-bound, or both.

Im folgenden Beispiel wird gezeigt, wie Sie TaskCompletionSource<TResult> verwenden, um einen Satz asynchroner WebClient-Vorgänge für Clientcode als grundlegende Task<TResult> verfügbar zu machen.The following example shows how to use a TaskCompletionSource<TResult> to expose a set of asynchronous WebClient operations to client code as a basic Task<TResult>. Die Methode ermöglichen die Eingabe eines Arrays von Web-URLs und eines zu suchenden Begriffs oder Namens und geben dann an, wie oft der Suchbegriff in jeder Site vorkommt.The method lets you enter an array of Web URLs, and a term or name to search for, and then returns the number of times the search term occurs on each site.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading;
using System.Threading.Tasks;

public class SimpleWebExample
{
  public Task<string[]> GetWordCountsSimplified(string[] urls, string name,
                                                CancellationToken token)
  {
      TaskCompletionSource<string[]> tcs = new TaskCompletionSource<string[]>();
      WebClient[] webClients = new WebClient[urls.Length];
      object m_lock = new object();
      int count = 0;
      List<string> results = new List<string>();

      // If the user cancels the CancellationToken, then we can use the
      // WebClient's ability to cancel its own async operations.
      token.Register(() =>
      {
          foreach (var wc in webClients)
          {
              if (wc != null)
                  wc.CancelAsync();
          }
      });


      for (int i = 0; i < urls.Length; i++)
      {
          webClients[i] = new WebClient();

          #region callback
          // Specify the callback for the DownloadStringCompleted
          // event that will be raised by this WebClient instance.
          webClients[i].DownloadStringCompleted += (obj, args) =>
          {

              // Argument validation and exception handling omitted for brevity.

              // Split the string into an array of words,
              // then count the number of elements that match
              // the search term.
              string[] words = args.Result.Split(' ');
              string NAME = name.ToUpper();
              int nameCount = (from word in words.AsParallel()
                               where word.ToUpper().Contains(NAME)
                               select word)
                              .Count();

              // Associate the results with the url, and add new string to the array that
              // the underlying Task object will return in its Result property.
              lock (m_lock)
              {
                 results.Add(String.Format("{0} has {1} instances of {2}", args.UserState, nameCount, name));

                 // If this is the last async operation to complete,
                 // then set the Result property on the underlying Task.
                 count++;
                 if (count == urls.Length)
                 {
                    tcs.TrySetResult(results.ToArray());
                 }
              }
          };
          #endregion

          // Call DownloadStringAsync for each URL.
          Uri address = null;
          address = new Uri(urls[i]);
          webClients[i].DownloadStringAsync(address, address);

      } // end for

      // Return the underlying Task. The client code
      // waits on the Result property, and handles exceptions
      // in the try-catch block there.
      return tcs.Task;
   }
}
Imports System.Collections.Generic
Imports System.Net
Imports System.Threading
Imports System.Threading.Tasks

Public Class SimpleWebExample
   Dim tcs As New TaskCompletionSource(Of String())
   Dim token As CancellationToken
   Dim results As New List(Of String)
   Dim m_lock As New Object()
   Dim count As Integer
   Dim addresses() As String
   Dim nameToSearch As String
   
   Public Function GetWordCountsSimplified(ByVal urls() As String, ByVal str As String,
                                           ByVal token As CancellationToken) As Task(Of String())
      addresses = urls
      nameToSearch = str
      
      Dim webClients(urls.Length - 1) As WebClient

      ' If the user cancels the CancellationToken, then we can use the
      ' WebClient's ability to cancel its own async operations.
      token.Register(Sub()
                        For Each wc As WebClient In webClients
                           If wc IsNot Nothing Then
                              wc.CancelAsync()
                           End If
                        Next
                     End Sub)

      For i As Integer = 0 To urls.Length - 1
         webClients(i) = New WebClient()

         ' Specify the callback for the DownloadStringCompleted
         ' event that will be raised by this WebClient instance.
         AddHandler webClients(i).DownloadStringCompleted, AddressOf WebEventHandler

         Dim address As New Uri(urls(i))
         ' Pass the address, and also use it for the userToken
         ' to identify the page when the delegate is invoked.
         webClients(i).DownloadStringAsync(address, address)
      Next

      ' Return the underlying Task. The client code
      ' waits on the Result property, and handles exceptions
      ' in the try-catch block there.
      Return tcs.Task
   End Function

   Public Sub WebEventHandler(ByVal sender As Object, ByVal args As DownloadStringCompletedEventArgs)

      If args.Cancelled = True Then
         tcs.TrySetCanceled()
         Return
      ElseIf args.Error IsNot Nothing Then
         tcs.TrySetException(args.Error)
         Return
      Else
         ' Split the string into an array of words,
         ' then count the number of elements that match
         ' the search term.
         Dim words() As String = args.Result.Split(" "c)

         Dim name As String = nameToSearch.ToUpper()
         Dim nameCount = (From word In words.AsParallel()
                          Where word.ToUpper().Contains(name)
                          Select word).Count()

         ' Associate the results with the url, and add new string to the array that
         ' the underlying Task object will return in its Result property.
         SyncLock (m_lock)
            results.Add(String.Format("{0} has {1} instances of {2}", args.UserState, nameCount, nameToSearch))
            count = count + 1
            If (count = addresses.Length) Then
               tcs.TrySetResult(results.ToArray())
            End If
         End SyncLock
      End If
   End Sub
End Class

Ein umfassenderes Beispiel, in dem auch die Ausnahmebehandlung und das Aufrufen der Methode im Clientcode beschrieben wird, finden Sie unter Vorgehensweise: Umschließen von EAP-Mustern in einer Aufgabe.For a more complete example, which includes additional exception handling and shows how to call the method from client code, see How to: Wrap EAP Patterns in a Task.

Beachten Sie, dass alle durch TaskCompletionSource<TResult> erstellten Aufgaben von dieser TaskCompletionSource gestartet werden. Der Benutzercode sollte daher für diese Aufgaben nicht die Start-Methode aufrufen.Remember that any task that is created by a TaskCompletionSource<TResult> will be started by that TaskCompletionSource and, therefore, user code should not call the Start method on that task.

Implementieren des APM-Musters mit AufgabenImplementing the APM Pattern By Using Tasks

In einigen Fallen ist es möglicherweise wünschenswert, das IAsyncResult-Muster durch Verwendung von Begin/End-Methodenpaaren in einer API direkt verfügbar zu machen.In some scenarios, it may be desirable to directly expose the IAsyncResult pattern by using Begin/End method pairs in an API. Dies kann beispielsweise erforderlich sein, wenn Sie die Konsistenz mit vorhandenen APIs aufrecht erhalten möchten oder wenn Sie über automatisierte Tools verfügen, die dieses Muster voraussetzen.For example, you may want to maintain consistency with existing APIs, or you may have automated tools that require this pattern. In diesen Fällen können Sie mithilfe von Aufgaben die interne Implementierung des APM-Musters vereinfachen.In such cases, you can use Tasks to simplify how the APM pattern is implemented internally.

Im folgenden Beispiel wird gezeigt, wie Sie mithilfe von Aufgaben ein APM Begin/End-Methodenpaar für eine rechnergebundene Methode mit langer Laufzeit implementieren.The following example shows how to use tasks to implement an APM Begin/End method pair for a long-running compute-bound method.

class Calculator
{
    public IAsyncResult BeginCalculate(int decimalPlaces, AsyncCallback ac, object state)
    {
        Console.WriteLine("Calling BeginCalculate on thread {0}", Thread.CurrentThread.ManagedThreadId);
        Task<string> f = Task<string>.Factory.StartNew(_ => Compute(decimalPlaces), state);
        if (ac != null) f.ContinueWith((res) => ac(f));
        return f;
    }

    public string Compute(int numPlaces)
    {
        Console.WriteLine("Calling compute on thread {0}", Thread.CurrentThread.ManagedThreadId);

        // Simulating some heavy work.
        Thread.SpinWait(500000000);

        // Actual implemenation left as exercise for the reader.
        // Several examples are available on the Web.
        return "3.14159265358979323846264338327950288";
    }

    public string EndCalculate(IAsyncResult ar)
    {
        Console.WriteLine("Calling EndCalculate on thread {0}", Thread.CurrentThread.ManagedThreadId);
        return ((Task<string>)ar).Result;
    }
}

public class CalculatorClient
{
    static int decimalPlaces = 12;
    public static void Main()
    {
        Calculator calc = new Calculator();
        int places = 35;

        AsyncCallback callBack = new AsyncCallback(PrintResult);
        IAsyncResult ar = calc.BeginCalculate(places, callBack, calc);

        // Do some work on this thread while the calulator is busy.
        Console.WriteLine("Working...");
        Thread.SpinWait(500000);
        Console.ReadLine();
    }

    public static void PrintResult(IAsyncResult result)
    {
        Calculator c = (Calculator)result.AsyncState;
        string piString = c.EndCalculate(result);
        Console.WriteLine("Calling PrintResult on thread {0}; result = {1}",
                    Thread.CurrentThread.ManagedThreadId, piString);
    }
}
Class Calculator
    Public Function BeginCalculate(ByVal decimalPlaces As Integer, ByVal ac As AsyncCallback, ByVal state As Object) As IAsyncResult
        Console.WriteLine("Calling BeginCalculate on thread {0}", Thread.CurrentThread.ManagedThreadId)
        Dim myTask = Task(Of String).Factory.StartNew(Function(obj) Compute(decimalPlaces), state)
        myTask.ContinueWith(Sub(antedecent) ac(myTask))

    End Function
    Private Function Compute(ByVal decimalPlaces As Integer)
        Console.WriteLine("Calling compute on thread {0}", Thread.CurrentThread.ManagedThreadId)

        ' Simulating some heavy work.
        Thread.SpinWait(500000000)

        ' Actual implemenation left as exercise for the reader.
        ' Several examples are available on the Web.
        Return "3.14159265358979323846264338327950288"
    End Function

    Public Function EndCalculate(ByVal ar As IAsyncResult) As String
        Console.WriteLine("Calling EndCalculate on thread {0}", Thread.CurrentThread.ManagedThreadId)
        Return CType(ar, Task(Of String)).Result
    End Function
End Class

Class CalculatorClient
    Shared decimalPlaces As Integer
    Shared Sub Main()
        Dim calc As New Calculator
        Dim places As Integer = 35
        Dim callback As New AsyncCallback(AddressOf PrintResult)
        Dim ar As IAsyncResult = calc.BeginCalculate(places, callback, calc)

        ' Do some work on this thread while the calulator is busy.
        Console.WriteLine("Working...")
        Thread.SpinWait(500000)
        Console.ReadLine()
    End Sub

    Public Shared Sub PrintResult(ByVal result As IAsyncResult)
        Dim c As Calculator = CType(result.AsyncState, Calculator)
        Dim piString As String = c.EndCalculate(result)
        Console.WriteLine("Calling PrintResult on thread {0}; result = {1}",
                   Thread.CurrentThread.ManagedThreadId, piString)
    End Sub

End Class

Verwenden des StreamExtensions-BeispielcodesUsing the StreamExtensions Sample Code

Die Datei „Streamextensions.cs“ unter Beispiele für die parallele Programmierung mit .NET Framework 4 enthält mehrere Referenzimplementierungen, in denen Aufgabenobjekte für asynchrone Datei- und Netzwerk-E/A verwendet werden.The Streamextensions.cs file, in Samples for Parallel Programming with the .NET Framework 4, contains several reference implementations that use Task objects for asynchronous file and network I/O.

Siehe auchSee also