Task<TResult>.ContinueWith Méthode

Définition

Crée une tâche de continuation qui s’exécute quand une autre tâche se termine.

Surcharges

Nom Description
ContinueWith(Action<Task<TResult>,Object>, Object, CancellationToken, TaskContinuationOptions, TaskScheduler)

Crée une continuation qui s’exécute une fois la cible Task<TResult> terminée.

ContinueWith(Action<Task<TResult>>, CancellationToken, TaskContinuationOptions, TaskScheduler)

Crée une continuation qui s’exécute en fonction de la condition spécifiée dans continuationOptions.

ContinueWith(Action<Task<TResult>,Object>, Object, TaskScheduler)

Crée une continuation qui s’exécute une fois la cible Task<TResult> terminée.

ContinueWith(Action<Task<TResult>,Object>, Object, CancellationToken)

Crée une continuation qui s’exécute une fois la cible Task<TResult> terminée.

ContinueWith(Action<Task<TResult>,Object>, Object, TaskContinuationOptions)

Crée une continuation qui s’exécute une fois la cible Task<TResult> terminée.

ContinueWith(Action<Task<TResult>>, TaskContinuationOptions)

Crée une continuation qui s’exécute en fonction de la condition spécifiée dans continuationOptions.

ContinueWith(Action<Task<TResult>>, CancellationToken)

Crée une continuation annulable qui s’exécute de façon asynchrone lorsque la cible Task<TResult> se termine.

ContinueWith(Action<Task<TResult>,Object>, Object)

Crée une continuation qui est transmise aux informations d’état et qui s’exécute une fois la cible Task<TResult> terminée.

ContinueWith(Action<Task<TResult>>)

Crée une continuation qui s’exécute de manière asynchrone lorsque la tâche cible se termine.

ContinueWith(Action<Task<TResult>>, TaskScheduler)

Crée une continuation qui s’exécute de manière asynchrone une fois la cible Task<TResult> terminée.

ContinueWith<TNewResult>(Func<Task<TResult>,Object,TNewResult>, Object, CancellationToken, TaskContinuationOptions, TaskScheduler)

Crée une continuation qui s’exécute une fois la cible Task<TResult> terminée.

ContinueWith<TNewResult>(Func<Task<TResult>,TNewResult>, CancellationToken, TaskContinuationOptions, TaskScheduler)

Crée une continuation qui s’exécute en fonction de la condition spécifiée dans continuationOptions.

ContinueWith<TNewResult>(Func<Task<TResult>,Object,TNewResult>, Object, TaskScheduler)

Crée une continuation qui s’exécute une fois la cible Task<TResult> terminée.

ContinueWith<TNewResult>(Func<Task<TResult>,Object,TNewResult>, Object, TaskContinuationOptions)

Crée une continuation qui s’exécute une fois la cible Task<TResult> terminée.

ContinueWith<TNewResult>(Func<Task<TResult>,Object,TNewResult>, Object, CancellationToken)

Crée une continuation qui s’exécute une fois la cible Task<TResult> terminée.

ContinueWith<TNewResult>(Func<Task<TResult>,TNewResult>, TaskContinuationOptions)

Crée une continuation qui s’exécute en fonction de la condition spécifiée dans continuationOptions.

ContinueWith<TNewResult>(Func<Task<TResult>,TNewResult>, CancellationToken)

Crée une continuation qui s’exécute de manière asynchrone une fois la cible Task<TResult> terminée.

ContinueWith<TNewResult>(Func<Task<TResult>,Object,TNewResult>, Object)

Crée une continuation qui s’exécute une fois la cible Task<TResult> terminée.

ContinueWith<TNewResult>(Func<Task<TResult>,TNewResult>)

Crée une continuation qui s’exécute de manière asynchrone une fois la cible Task<TResult> terminée.

ContinueWith<TNewResult>(Func<Task<TResult>,TNewResult>, TaskScheduler)

Crée une continuation qui s’exécute de manière asynchrone une fois la cible Task<TResult> terminée.

ContinueWith(Action<Task<TResult>,Object>, Object, CancellationToken, TaskContinuationOptions, TaskScheduler)

Crée une continuation qui s’exécute une fois la cible Task<TResult> terminée.

public:
 System::Threading::Tasks::Task ^ ContinueWith(Action<System::Threading::Tasks::Task<TResult> ^, System::Object ^> ^ continuationAction, System::Object ^ state, System::Threading::CancellationToken cancellationToken, System::Threading::Tasks::TaskContinuationOptions continuationOptions, System::Threading::Tasks::TaskScheduler ^ scheduler);
public System.Threading.Tasks.Task ContinueWith(Action<System.Threading.Tasks.Task<TResult>,object> continuationAction, object state, System.Threading.CancellationToken cancellationToken, System.Threading.Tasks.TaskContinuationOptions continuationOptions, System.Threading.Tasks.TaskScheduler scheduler);
override this.ContinueWith : Action<System.Threading.Tasks.Task<'Result>, obj> * obj * System.Threading.CancellationToken * System.Threading.Tasks.TaskContinuationOptions * System.Threading.Tasks.TaskScheduler -> System.Threading.Tasks.Task
Public Function ContinueWith (continuationAction As Action(Of Task(Of TResult), Object), state As Object, cancellationToken As CancellationToken, continuationOptions As TaskContinuationOptions, scheduler As TaskScheduler) As Task

Paramètres

continuationAction
Action<Task<TResult>,Object>

Action à exécuter une fois l’opération Task<TResult> terminée. Lors de l’exécution, le délégué est passé la tâche terminée et l’objet d’état fourni par l’appelant en tant qu’arguments.

state
Object

Objet représentant les données à utiliser par l’action de continuation.

cancellationToken
CancellationToken

Qui CancellationToken sera affecté à la nouvelle tâche de continuation.

continuationOptions
TaskContinuationOptions

Options pour le moment où la continuation est planifiée et comment elle se comporte. Cela inclut des critères, tels que OnlyOnCanceled, ainsi que des options d’exécution, telles que ExecuteSynchronously.

scheduler
TaskScheduler

À TaskScheduler associer à la tâche de continuation et à utiliser pour son exécution.

Retours

Une nouvelle continuation Task.

Exceptions

L’argument scheduler est null.

L’argument continuationOptions spécifie une valeur non valide pour TaskContinuationOptions.

Les données fournies CancellationToken ont déjà été supprimées.

Remarques

Le retour Task ne sera pas planifié pour l’exécution tant que la tâche en cours n’est pas terminée. Si les critères spécifiés par le biais du continuationOptions paramètre ne sont pas remplis, la tâche de continuation est annulée au lieu d’être planifiée.

Voir aussi

S’applique à

ContinueWith(Action<Task<TResult>>, CancellationToken, TaskContinuationOptions, TaskScheduler)

Crée une continuation qui s’exécute en fonction de la condition spécifiée dans continuationOptions.

public:
 System::Threading::Tasks::Task ^ ContinueWith(Action<System::Threading::Tasks::Task<TResult> ^> ^ continuationAction, System::Threading::CancellationToken cancellationToken, System::Threading::Tasks::TaskContinuationOptions continuationOptions, System::Threading::Tasks::TaskScheduler ^ scheduler);
public System.Threading.Tasks.Task ContinueWith(Action<System.Threading.Tasks.Task<TResult>> continuationAction, System.Threading.CancellationToken cancellationToken, System.Threading.Tasks.TaskContinuationOptions continuationOptions, System.Threading.Tasks.TaskScheduler scheduler);
override this.ContinueWith : Action<System.Threading.Tasks.Task<'Result>> * System.Threading.CancellationToken * System.Threading.Tasks.TaskContinuationOptions * System.Threading.Tasks.TaskScheduler -> System.Threading.Tasks.Task
Public Function ContinueWith (continuationAction As Action(Of Task(Of TResult)), cancellationToken As CancellationToken, continuationOptions As TaskContinuationOptions, scheduler As TaskScheduler) As Task

Paramètres

continuationAction
Action<Task<TResult>>

Action à exécuter en fonction de la condition spécifiée dans continuationOptions. Lors de l’exécution, le délégué est passé la tâche terminée en tant qu’argument.

cancellationToken
CancellationToken

Qui CancellationToken sera affecté à la nouvelle tâche de continuation.

continuationOptions
TaskContinuationOptions

Options pour le moment où la continuation est planifiée et comment elle se comporte. Cela inclut des critères, tels que OnlyOnCanceled, ainsi que des options d’exécution, telles que ExecuteSynchronously.

scheduler
TaskScheduler

À TaskScheduler associer à la tâche de continuation et à utiliser pour son exécution.

Retours

Une nouvelle continuation Task.

Exceptions

Il Task<TResult> a été supprimé.

-ou-

Celui CancellationTokenSource créé cancellationToken a déjà été supprimé.

L’argument continuationAction est null.

-ou-

L’argument scheduler est null.

L’argument continuationOptions spécifie une valeur non valide pour TaskContinuationOptions.

Remarques

Le retour Task ne sera pas planifié pour l’exécution tant que la tâche en cours n’est pas terminée. Si les critères spécifiés par le biais du continuationOptions paramètre ne sont pas remplis, la tâche de continuation est annulée au lieu d’être planifiée. Pour plus d’informations, consultez Chaînage des tâches à l’aide de tâches de continuation.

Voir aussi

S’applique à

ContinueWith(Action<Task<TResult>,Object>, Object, TaskScheduler)

Crée une continuation qui s’exécute une fois la cible Task<TResult> terminée.

public:
 System::Threading::Tasks::Task ^ ContinueWith(Action<System::Threading::Tasks::Task<TResult> ^, System::Object ^> ^ continuationAction, System::Object ^ state, System::Threading::Tasks::TaskScheduler ^ scheduler);
public System.Threading.Tasks.Task ContinueWith(Action<System.Threading.Tasks.Task<TResult>,object> continuationAction, object state, System.Threading.Tasks.TaskScheduler scheduler);
override this.ContinueWith : Action<System.Threading.Tasks.Task<'Result>, obj> * obj * System.Threading.Tasks.TaskScheduler -> System.Threading.Tasks.Task
Public Function ContinueWith (continuationAction As Action(Of Task(Of TResult), Object), state As Object, scheduler As TaskScheduler) As Task

Paramètres

continuationAction
Action<Task<TResult>,Object>

Action à exécuter une fois l’opération Task<TResult> terminée. Lors de l’exécution, le délégué est passé la tâche terminée et l’objet d’état fourni par l’appelant en tant qu’arguments.

state
Object

Objet représentant les données à utiliser par l’action de continuation.

scheduler
TaskScheduler

À TaskScheduler associer à la tâche de continuation et à utiliser pour son exécution.

Retours

Une nouvelle continuation Task.

Exceptions

L’argument scheduler est null.

Remarques

Le retour Task ne sera pas planifié pour l’exécution tant que la tâche en cours n’est pas terminée, qu’elle soit terminée en raison d’une exécution réussie, d’une erreur en raison d’une exception non gérée ou de la sortie anticipée en raison d’une annulation.

Voir aussi

S’applique à

ContinueWith(Action<Task<TResult>,Object>, Object, CancellationToken)

Crée une continuation qui s’exécute une fois la cible Task<TResult> terminée.

public:
 System::Threading::Tasks::Task ^ ContinueWith(Action<System::Threading::Tasks::Task<TResult> ^, System::Object ^> ^ continuationAction, System::Object ^ state, System::Threading::CancellationToken cancellationToken);
public System.Threading.Tasks.Task ContinueWith(Action<System.Threading.Tasks.Task<TResult>,object> continuationAction, object state, System.Threading.CancellationToken cancellationToken);
override this.ContinueWith : Action<System.Threading.Tasks.Task<'Result>, obj> * obj * System.Threading.CancellationToken -> System.Threading.Tasks.Task
Public Function ContinueWith (continuationAction As Action(Of Task(Of TResult), Object), state As Object, cancellationToken As CancellationToken) As Task

Paramètres

continuationAction
Action<Task<TResult>,Object>

Action à exécuter une fois l’opération Task<TResult> terminée. Lors de l’exécution, le délégué est passé la tâche terminée et l’objet d’état fourni par l’appelant en tant qu’arguments.

state
Object

Objet représentant les données à utiliser par l’action de continuation.

cancellationToken
CancellationToken

Qui CancellationToken sera affecté à la nouvelle tâche de continuation.

Retours

Une nouvelle continuation Task.

Exceptions

L’argument continuationAction est null.

Les données fournies CancellationToken ont déjà été supprimées.

Remarques

Le retour Task ne sera pas planifié pour l’exécution tant que la tâche en cours n’est pas terminée, qu’elle soit terminée en raison d’une exécution réussie, d’une erreur en raison d’une exception non gérée ou de la sortie anticipée en raison d’une annulation.

Voir aussi

S’applique à

ContinueWith(Action<Task<TResult>,Object>, Object, TaskContinuationOptions)

Crée une continuation qui s’exécute une fois la cible Task<TResult> terminée.

public:
 System::Threading::Tasks::Task ^ ContinueWith(Action<System::Threading::Tasks::Task<TResult> ^, System::Object ^> ^ continuationAction, System::Object ^ state, System::Threading::Tasks::TaskContinuationOptions continuationOptions);
public System.Threading.Tasks.Task ContinueWith(Action<System.Threading.Tasks.Task<TResult>,object> continuationAction, object state, System.Threading.Tasks.TaskContinuationOptions continuationOptions);
override this.ContinueWith : Action<System.Threading.Tasks.Task<'Result>, obj> * obj * System.Threading.Tasks.TaskContinuationOptions -> System.Threading.Tasks.Task
Public Function ContinueWith (continuationAction As Action(Of Task(Of TResult), Object), state As Object, continuationOptions As TaskContinuationOptions) As Task

Paramètres

continuationAction
Action<Task<TResult>,Object>

Action à exécuter une fois l’opération Task<TResult> terminée. Lors de l’exécution, le délégué est passé la tâche terminée et l’objet d’état fourni par l’appelant en tant qu’arguments.

state
Object

Objet représentant les données à utiliser par l’action de continuation.

continuationOptions
TaskContinuationOptions

Options pour le moment où la continuation est planifiée et comment elle se comporte. Cela inclut des critères, tels que OnlyOnCanceled, ainsi que des options d’exécution, telles que ExecuteSynchronously.

Retours

Une nouvelle continuation Task.

Exceptions

L’argument continuationAction est null.

L’argument continuationOptions spécifie une valeur non valide pour TaskContinuationOptions.

Remarques

Le retour Task ne sera pas planifié pour l’exécution tant que la tâche en cours n’est pas terminée. Si les critères de continuation spécifiés par le biais du continuationOptions paramètre ne sont pas remplis, la tâche de continuation est annulée au lieu d’être planifiée.

Voir aussi

S’applique à

ContinueWith(Action<Task<TResult>>, TaskContinuationOptions)

Crée une continuation qui s’exécute en fonction de la condition spécifiée dans continuationOptions.

public:
 System::Threading::Tasks::Task ^ ContinueWith(Action<System::Threading::Tasks::Task<TResult> ^> ^ continuationAction, System::Threading::Tasks::TaskContinuationOptions continuationOptions);
public System.Threading.Tasks.Task ContinueWith(Action<System.Threading.Tasks.Task<TResult>> continuationAction, System.Threading.Tasks.TaskContinuationOptions continuationOptions);
override this.ContinueWith : Action<System.Threading.Tasks.Task<'Result>> * System.Threading.Tasks.TaskContinuationOptions -> System.Threading.Tasks.Task
Public Function ContinueWith (continuationAction As Action(Of Task(Of TResult)), continuationOptions As TaskContinuationOptions) As Task

Paramètres

continuationAction
Action<Task<TResult>>

Action selon la condition spécifiée dans continuationOptions. Lors de l’exécution, le délégué est passé la tâche terminée en tant qu’argument.

continuationOptions
TaskContinuationOptions

Options pour le moment où la continuation est planifiée et comment elle se comporte. Cela inclut des critères, tels que OnlyOnCanceled, ainsi que des options d’exécution, telles que ExecuteSynchronously.

Retours

Une nouvelle continuation Task.

Exceptions

Il Task<TResult> a été supprimé.

L’argument continuationAction est null.

L’argument continuationOptions spécifie une valeur non valide pour TaskContinuationOptions.

Remarques

Le retour Task ne sera pas planifié pour l’exécution tant que la tâche en cours n’est pas terminée. Si les critères de continuation spécifiés par le biais du continuationOptions paramètre ne sont pas remplis, la tâche de continuation est annulée au lieu d’être planifiée.

Pour plus d’informations, consultez Chaînage des tâches à l’aide de tâches de continuation.

Voir aussi

S’applique à

ContinueWith(Action<Task<TResult>>, CancellationToken)

Crée une continuation annulable qui s’exécute de façon asynchrone lorsque la cible Task<TResult> se termine.

public:
 System::Threading::Tasks::Task ^ ContinueWith(Action<System::Threading::Tasks::Task<TResult> ^> ^ continuationAction, System::Threading::CancellationToken cancellationToken);
public System.Threading.Tasks.Task ContinueWith(Action<System.Threading.Tasks.Task<TResult>> continuationAction, System.Threading.CancellationToken cancellationToken);
override this.ContinueWith : Action<System.Threading.Tasks.Task<'Result>> * System.Threading.CancellationToken -> System.Threading.Tasks.Task
Public Function ContinueWith (continuationAction As Action(Of Task(Of TResult)), cancellationToken As CancellationToken) As Task

Paramètres

continuationAction
Action<Task<TResult>>

Action à exécuter une fois l’opération Task<TResult> terminée. Lors de l’exécution, le délégué est passé la tâche terminée en tant qu’argument.

cancellationToken
CancellationToken

Jeton d’annulation passé à la nouvelle tâche de continuation.

Retours

Nouvelle tâche de continuation.

Exceptions

Il Task<TResult> a été supprimé.

-ou-

Celui CancellationTokenSource créé cancellationToken a été supprimé.

L’argument continuationAction est null.

Exemples

L’exemple suivant crée une tâche antécédente qui utilise le sieve d’Eratosthenes pour calculer les nombres premiers compris entre 1 et une valeur entrée par l’utilisateur. Un tableau est utilisé pour contenir des informations sur les nombres premiers. L’index de tableau représente le nombre et la valeur de l’élément indique si ce nombre est composite (sa valeur est true) ou prime (sa valeur est false). Cette tâche est ensuite passée à une tâche de continuation, qui est chargée d’extraire les nombres premiers du tableau entier et de les afficher.

Un jeton d’annulation est transmis à la fois à l’antécédent et à la tâche de continuation. Un System.Timers.Timer objet est utilisé pour définir une valeur de délai d’expiration de 100 millisecondes. Si l’événement se déclenche, la CancellationTokenSource.Cancel méthode est appelée et le jeton d’annulation est utilisé pour demander l’annulation des tâches.

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Timers = System.Timers;

public class Example
{
   static CancellationTokenSource ts;
   
   public static void Main(string[] args)
   {
      int upperBound = args.Length >= 1 ? Int32.Parse(args[0]) : 200;
      ts = new CancellationTokenSource();
      CancellationToken token = ts.Token;
      Timers.Timer timer = new Timers.Timer(3000);
      timer.Elapsed += TimedOutEvent;
      timer.AutoReset = false;
      timer.Enabled = true;

      var t1 = Task.Run(() => { // True = composite.
                                // False = prime.
                                bool[] values = new bool[upperBound + 1];
                                for (int ctr = 2; ctr <= (int) Math.Sqrt(upperBound); ctr++) {
                                   if (!values[ctr] ) {
                                      for (int product = ctr * ctr; product <= upperBound;
                                                                    product = product + ctr)
                                         values[product] = true;
                                   }
                                   token.ThrowIfCancellationRequested();
                                }
                                return values; }, token);

      var t2 = t1.ContinueWith( (antecedent) => { // Create a list of prime numbers.
                                                  var  primes = new List<int>();
                                                  token.ThrowIfCancellationRequested();
                                                  bool[] numbers = antecedent.Result;
                                                  string output = String.Empty;

                                                  for (int ctr = 1; ctr <= numbers.GetUpperBound(0); ctr++)
                                                     if (!numbers[ctr] )
                                                        primes.Add(ctr);

                                                  // Create the output string.
                                                  for (int ctr = 0; ctr < primes.Count; ctr++) {
                                                     token.ThrowIfCancellationRequested();
                                                     output += primes[ctr].ToString("N0");
                                                     if (ctr < primes.Count - 1)
                                                        output += ",  ";
                                                     if ((ctr + 1) % 8 == 0)
                                                        output += Environment.NewLine;
                                                  }
                                                  //Display the result.
                                                  Console.WriteLine("Prime numbers from 1 to {0}:\n",
                                                                    upperBound);
                                                  Console.WriteLine(output);
                                                }, token);
      try {
         t2.Wait();
      }
      catch (AggregateException ae) {
         foreach (var e in ae.InnerExceptions) {
            if (e.GetType() == typeof(TaskCanceledException))
               Console.WriteLine("The operation was cancelled.");
            else
               Console.WriteLine("ELSE: {0}: {1}", e.GetType().Name, e.Message);
         }
      }
      finally {
         ts.Dispose();
      }
   }

   private static void TimedOutEvent(Object source, Timers.ElapsedEventArgs e)
   {
      ts.Cancel();
   }
}
// If cancellation is not requested, the example displays output like the following:
//       Prime numbers from 1 to 400:
//
//       1,  2,  3,  5,  7,  11,  13,  17,
//       19,  23,  29,  31,  37,  41,  43,  47,
//       53,  59,  61,  67,  71,  73,  79,  83,
//       89,  97,  101,  103,  107,  109,  113,  127,
//       131,  137,  139,  149,  151,  157,  163,  167,
//       173,  179,  181,  191,  193,  197,  199,  211,
//       223,  227,  229,  233,  239,  241,  251,  257,
//       263,  269,  271,  277,  281,  283,  293,  307,
//       311,  313,  317,  331,  337,  347,  349,  353,
//       359,  367,  373,  379,  383,  389,  397,  401
// If cancellation is requested, the example displays output like the following:
//       The operation was cancelled.
Imports System.Collections.Generic
Imports System.Threading
Imports System.Threading.Tasks
Imports Timers = System.Timers

Module Example
   Dim ts As CancellationTokenSource

   Public Sub Main(args() As String)
      Dim upperBound As Integer = If(args.Length >= 1, CInt(args(0)), 200)
      ts = New CancellationTokenSource()
      Dim token As CancellationToken = ts.Token
      Dim timer As New Timers.Timer(100)
      AddHandler timer.Elapsed, AddressOf TimedOutEvent
      timer.AutoReset = False
      timer.Enabled = True

      Dim t1 = Task.Run(Function()
                          ' True = composite.
                          ' False = prime.
                          Dim values(upperBound) As Boolean
                          For ctr = 2 To CInt(Math.Sqrt(upperBound))
                             If values(ctr) = False Then
                                For product = ctr * ctr To upperBound Step ctr
                                   values(product) = True
                                Next
                             End If
                             token.ThrowIfCancellationRequested()
                          Next
                          Return values
                       End Function, token)

      Dim t2 = t1.ContinueWith(Sub(antecedent)
                                  ' Create a list of prime numbers.
                                  Dim primes As New List(Of Integer)()
                                  token.ThrowIfCancellationRequested()
                                  Dim numbers As Boolean() = antecedent.Result
                                  Dim output As String = String.Empty
                                  
                                  For ctr As Integer = 1 To numbers.GetUpperBound(0)
                                     If numbers(ctr) = False Then primes.Add(ctr)
                                  Next

                                  ' Create the output string.
                                  For ctr As Integer = 0 To primes.Count - 1
                                     token.ThrowIfCancellationRequested()
                                     output += primes(ctr).ToString("N0")
                                     If ctr < primes.Count - 1 Then output += ",  "
                                     If (ctr + 1) Mod 8 = 0 Then output += vbCrLf
                                  Next
                                  'Display the result.
                                  Console.WriteLine("Prime numbers from 1 to {0}:{1}",
                                                    upperBound, vbCrLf)
                                  Console.WriteLine(output)
                               End Sub, token)
      Try
         t2.Wait()
      Catch ae As AggregateException
         For Each e In ae.InnerExceptions
            If e.GetType Is GetType(TaskCanceledException) Then
               Console.WriteLine("The operation was cancelled.")
            Else
               Console.WriteLine("{0}: {1}", e.GetType().Name, e.Message)
            End If
         Next
      Finally
         ts.Dispose()
      End Try
   End Sub
   
   Private Sub TimedOutEvent(source As Object, e As Timers.ElapsedEventArgs)
      ts.Cancel()
   End Sub
End Module
' If cancellation is not requested, the example displays output like the following:
'       Prime numbers from 1 to 400:
'
'       1,  2,  3,  5,  7,  11,  13,  17,
'       19,  23,  29,  31,  37,  41,  43,  47,
'       53,  59,  61,  67,  71,  73,  79,  83,
'       89,  97,  101,  103,  107,  109,  113,  127,
'       131,  137,  139,  149,  151,  157,  163,  167,
'       173,  179,  181,  191,  193,  197,  199,  211,
'       223,  227,  229,  233,  239,  241,  251,  257,
'       263,  269,  271,  277,  281,  283,  293,  307,
'       311,  313,  317,  331,  337,  347,  349,  353,
'       359,  367,  373,  379,  383,  389,  397,  401
' If cancellation is requested, the example displays output like the following:
'       The operation was cancelled.

En règle générale, l’attribution d’une valeur d’environ 100 000 provoque l’expiration de l’intervalle de délai d’expiration et l’événement Timer.Elapsed à déclencher et la demande d’annulation à définir.

Remarques

Le retour Task ne sera pas planifié pour l’exécution tant que la tâche en cours n’est pas terminée, qu’elle soit terminée en raison d’une exécution réussie, d’une erreur en raison d’une exception non gérée ou de la sortie anticipée en raison d’une annulation.

Voir aussi

S’applique à

ContinueWith(Action<Task<TResult>,Object>, Object)

Crée une continuation qui est transmise aux informations d’état et qui s’exécute une fois la cible Task<TResult> terminée.

public:
 System::Threading::Tasks::Task ^ ContinueWith(Action<System::Threading::Tasks::Task<TResult> ^, System::Object ^> ^ continuationAction, System::Object ^ state);
public System.Threading.Tasks.Task ContinueWith(Action<System.Threading.Tasks.Task<TResult>,object> continuationAction, object state);
override this.ContinueWith : Action<System.Threading.Tasks.Task<'Result>, obj> * obj -> System.Threading.Tasks.Task
Public Function ContinueWith (continuationAction As Action(Of Task(Of TResult), Object), state As Object) As Task

Paramètres

continuationAction
Action<Task<TResult>,Object>

Action à exécuter une fois l’opération Task<TResult> terminée. Lors de l’exécution, le délégué est passé la tâche terminée et l’objet d’état fourni par l’appelant en tant qu’arguments.

state
Object

Objet représentant les données à utiliser par l’action de continuation.

Retours

Une nouvelle continuation Task.

Exceptions

L’argument continuationAction est null.

Exemples

L’exemple suivant crée une tâche qui passe un entier compris entre 2 et 20 et retourne un tableau qui contient les dix premiers exposants (de n1 à n10) de ce nombre. Une tâche de continuation est ensuite chargée d’afficher les exposants. Il est transmis à la fois l’antécédent et le nombre d’origine dont l’exposant génère l’antécédent.

using System;
using System.Threading;
using System.Threading.Tasks;

public class Example
{
   public static void Main()
   {
      var cts = new CancellationTokenSource();
      var token = cts.Token;

      // Get an integer to generate a list of its exponents.
      var rnd = new Random();
      var number = rnd.Next(2, 21);
      
      var t = Task.Factory.StartNew( (value) => { int n = (int) value;
                                                  long[] values = new long[10];
                                                  for (int ctr = 1; ctr <= 10; ctr++)
                                                     values[ctr - 1] = (long) Math.Pow(n, ctr);
                                                     
                                                  return values;
                                                }, number);
      var continuation = t.ContinueWith( (antecedent, value) => { Console.WriteLine("Exponents of {0}:", value);
                                                                  for (int ctr = 0; ctr <= 9; ctr++)
                                                                     Console.WriteLine("   {0} {1} {2} = {3:N0}",
                                                                                       value, "\u02C6", ctr + 1,
                                                                                       antecedent.Result[ctr]);
                                                                  Console.WriteLine();
                                                                }, number);
      continuation.Wait();
      cts.Dispose();
   }
}
// The example displays output like the following:
//       Exponents of 2:
//          2 ^ 1 = 2
//          2 ^ 2 = 4
//          2 ^ 3 = 8
//          2 ^ 4 = 16
//          2 ^ 5 = 32
//          2 ^ 6 = 64
//          2 ^ 7 = 128
//          2 ^ 8 = 256
//          2 ^ 9 = 512
//          2 ^ 10 = 1,024
Imports System.Threading
Imports System.Threading.Tasks

Module Example
   Public Sub Main()
      Dim cts As New CancellationTokenSource()
      Dim token As CancellationToken = cts.Token

      ' Get an integer to generate a list of its exponents.
      Dim rnd As New Random()
      Dim number As Integer = rnd.Next(2, 21)

      Dim t = Task.Factory.StartNew( Function(value)
                                        Dim n As Integer = CInt(value)
                                        Dim values(9) As Long
                                        For ctr As Integer = 1 To 10
                                           values(ctr - 1) = CLng(Math.Pow(n, ctr))
                                        Next
                                        return values
                                     End Function, number)
      Dim continuation = t.ContinueWith( Sub(antecedent, value)
                                            Console.WriteLine("Exponents of {0}:", value)
                                            For ctr As Integer = 0 To 9
                                               Console.WriteLine("   {0} {1} {2} = {3:N0}",
                                                                 value, ChrW(&h02C6), ctr + 1,
                                                                 antecedent.Result(ctr))
                                            Next
                                            Console.WriteLine()
                                         End Sub, number)
      continuation.Wait()

      cts.Dispose()
   End Sub
End Module
' The example displays output like the following:
'       Exponents of 2:
'          2 ^ 1 = 2
'          2 ^ 2 = 4
'          2 ^ 3 = 8
'          2 ^ 4 = 16
'          2 ^ 5 = 32
'          2 ^ 6 = 64
'          2 ^ 7 = 128
'          2 ^ 8 = 256
'          2 ^ 9 = 512
'          2 ^ 10 = 1,024

Remarques

Le retour Task ne sera pas planifié pour l’exécution tant que la tâche en cours n’est pas terminée, qu’elle soit terminée en raison d’une exécution réussie, d’une erreur en raison d’une exception non gérée ou de la sortie anticipée en raison d’une annulation.

Voir aussi

S’applique à

ContinueWith(Action<Task<TResult>>)

Crée une continuation qui s’exécute de manière asynchrone lorsque la tâche cible se termine.

public:
 System::Threading::Tasks::Task ^ ContinueWith(Action<System::Threading::Tasks::Task<TResult> ^> ^ continuationAction);
public System.Threading.Tasks.Task ContinueWith(Action<System.Threading.Tasks.Task<TResult>> continuationAction);
override this.ContinueWith : Action<System.Threading.Tasks.Task<'Result>> -> System.Threading.Tasks.Task
Public Function ContinueWith (continuationAction As Action(Of Task(Of TResult))) As Task

Paramètres

continuationAction
Action<Task<TResult>>

Action à exécuter lorsque l’antécédent Task<TResult> se termine. Lors de l’exécution, le délégué est passé la tâche terminée en tant qu’argument.

Retours

Nouvelle tâche de continuation.

Exceptions

Il Task<TResult> a été supprimé.

L’argument continuationAction est null.

Exemples

L’exemple suivant crée une tâche antécédente qui utilise le sieve d’Eratosthenes pour calculer les nombres premiers compris entre 1 et une valeur entrée par l’utilisateur. Un tableau est utilisé pour contenir des informations sur les nombres premiers. L’index de tableau représente le nombre et la valeur de l’élément indique si ce nombre est composite (sa valeur est true) ou prime (sa valeur est false). Cette tâche est ensuite passée à une tâche de continuation, qui est chargée d’extraire les nombres premiers du tableau entier et de les afficher.

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

public class Example
{
   public static void Main(string[] args)
   {
      int upperBound = args.Length >= 1 ? Int32.Parse(args[0]) : 200;

      var t1 = Task.Run(() => { // True = composite.
                                // False = prime.
                                bool[] values = new bool[upperBound + 1];
                                for (int ctr = 2; ctr <= (int) Math.Sqrt(upperBound); ctr++) {
                                   if (!values[ctr]) {
                                      for (int product = ctr * ctr; product <= upperBound;
                                                                    product = product + ctr)
                                         values[product] = true;
                                   }
                                }
                                return values; });
      var t2 = t1.ContinueWith( (antecedent) => { // Create a list of prime numbers.
                                                  var  primes = new List<int>();
                                                  bool[] numbers = antecedent.Result;
                                                  string output = String.Empty;

                                                  for (int ctr = 1; ctr <= numbers.GetUpperBound(0); ctr++)
                                                     if (!numbers[ctr])
                                                        primes.Add(ctr);

                                                  // Create the output string.
                                                  for (int ctr = 0; ctr < primes.Count; ctr++) {
                                                     output += primes[ctr].ToString("N0");
                                                     if (ctr < primes.Count - 1)
                                                        output += ",  ";
                                                     if ((ctr + 1) % 8 == 0)
                                                        output += Environment.NewLine;
                                                  }
                                                  //Display the result.
                                                  Console.WriteLine("Prime numbers from 1 to {0}:\n",
                                                                    upperBound);
                                                  Console.WriteLine(output);
                                                });
      try {
         t2.Wait();
      }
      catch (AggregateException ae) {
         foreach (var e in ae.InnerExceptions)
            Console.WriteLine("{0}: {1}", e.GetType().Name, e.Message);
      }
   }
}
// The example displays output like the following:
//       Prime numbers from 1 to 400:
//
//       1,  2,  3,  5,  7,  11,  13,  17,
//       19,  23,  29,  31,  37,  41,  43,  47,
//       53,  59,  61,  67,  71,  73,  79,  83,
//       89,  97,  101,  103,  107,  109,  113,  127,
//       131,  137,  139,  149,  151,  157,  163,  167,
//       173,  179,  181,  191,  193,  197,  199,  211,
//       223,  227,  229,  233,  239,  241,  251,  257,
//       263,  269,  271,  277,  281,  283,  293,  307,
//       311,  313,  317,  331,  337,  347,  349,  353,
//       359,  367,  373,  379,  383,  389,  397,  401
Imports System.Collections.Generic
Imports System.Threading
Imports System.Threading.Tasks

Module Example
   Public Sub Main(args() As String)
      Dim upperBound As Integer = If(args.Length >= 1, CInt(args(0)), 200)

      Dim t1 = Task.Run(Function()
                          ' True = composite.
                          ' False = prime.
                          Dim values(upperBound) As Boolean
                          For ctr = 2 To CInt(Math.Sqrt(upperBound))
                             If values(ctr) = False Then
                                For product = ctr * ctr To upperBound Step ctr
                                   values(product) = True
                                Next
                             End If
                          Next
                          Return values
                       End Function)

      Dim t2 = t1.ContinueWith(Sub(antecedent)
                                  ' Create a list of prime numbers.
                                  Dim primes As New List(Of Integer)()
                                  Dim numbers As Boolean() = antecedent.Result
                                  Dim output As String = String.Empty
                                  
                                  For ctr As Integer = 1 To numbers.GetUpperBound(0)
                                     If numbers(ctr) = False Then primes.Add(ctr)
                                  Next

                                  ' Create the output string.
                                  For ctr As Integer = 0 To primes.Count - 1
                                     output += primes(ctr).ToString("N0")
                                     If ctr < primes.Count - 1 Then output += ",  "
                                     If (ctr + 1) Mod 8 = 0 Then output += vbCrLf
                                  Next
                                  'Display the result.
                                  Console.WriteLine("Prime numbers from 1 to {0}:{1}",
                                                    upperBound, vbCrLf)
                                  Console.WriteLine(output)
                               End Sub)
      Try
         t2.Wait()
      Catch ae As AggregateException
         For Each e In ae.InnerExceptions
            Console.WriteLine("{0}: {1}", e.GetType().Name, e.Message)
         Next
      End Try
   End Sub
End Module
' The example displays output like the following:
'       Prime numbers from 1 to 400:
'
'       1,  2,  3,  5,  7,  11,  13,  17,
'       19,  23,  29,  31,  37,  41,  43,  47,
'       53,  59,  61,  67,  71,  73,  79,  83,
'       89,  97,  101,  103,  107,  109,  113,  127,
'       131,  137,  139,  149,  151,  157,  163,  167,
'       173,  179,  181,  191,  193,  197,  199,  211,
'       223,  227,  229,  233,  239,  241,  251,  257,
'       263,  269,  271,  277,  281,  283,  293,  307,
'       311,  313,  317,  331,  337,  347,  349,  353,
'       359,  367,  373,  379,  383,  389,  397,  401
' If cancellation is requested, the example displays output like the following:
'       The operation was cancelled.

Remarques

Le retour Task ne sera pas planifié pour l’exécution tant que la tâche actuelle n’est pas terminée, qu’elle soit terminée en raison d’une exécution réussie, d’une erreur en raison d’une exception non gérée ou d’une sortie anticipée en raison d’une annulation.

Voir aussi

S’applique à

ContinueWith(Action<Task<TResult>>, TaskScheduler)

Crée une continuation qui s’exécute de manière asynchrone une fois la cible Task<TResult> terminée.

public:
 System::Threading::Tasks::Task ^ ContinueWith(Action<System::Threading::Tasks::Task<TResult> ^> ^ continuationAction, System::Threading::Tasks::TaskScheduler ^ scheduler);
public System.Threading.Tasks.Task ContinueWith(Action<System.Threading.Tasks.Task<TResult>> continuationAction, System.Threading.Tasks.TaskScheduler scheduler);
override this.ContinueWith : Action<System.Threading.Tasks.Task<'Result>> * System.Threading.Tasks.TaskScheduler -> System.Threading.Tasks.Task
Public Function ContinueWith (continuationAction As Action(Of Task(Of TResult)), scheduler As TaskScheduler) As Task

Paramètres

continuationAction
Action<Task<TResult>>

Action à exécuter une fois l’opération Task<TResult> terminée. Lors de l’exécution, le délégué est passé la tâche terminée en tant qu’argument.

scheduler
TaskScheduler

À TaskScheduler associer à la tâche de continuation et à utiliser pour son exécution.

Retours

Une nouvelle continuation Task.

Exceptions

Il Task<TResult> a été supprimé.

L’argument continuationAction est null.

-ou-

L’argument scheduler est null.

Remarques

Le retour Task ne sera pas planifié pour l’exécution tant que la tâche en cours n’est pas terminée, qu’elle soit terminée en raison d’une exécution réussie, d’une erreur en raison d’une exception non gérée ou de la sortie anticipée en raison d’une annulation.

Voir aussi

S’applique à

ContinueWith<TNewResult>(Func<Task<TResult>,Object,TNewResult>, Object, CancellationToken, TaskContinuationOptions, TaskScheduler)

Crée une continuation qui s’exécute une fois la cible Task<TResult> terminée.

public:
generic <typename TNewResult>
 System::Threading::Tasks::Task<TNewResult> ^ ContinueWith(Func<System::Threading::Tasks::Task<TResult> ^, System::Object ^, TNewResult> ^ continuationFunction, System::Object ^ state, System::Threading::CancellationToken cancellationToken, System::Threading::Tasks::TaskContinuationOptions continuationOptions, System::Threading::Tasks::TaskScheduler ^ scheduler);
public System.Threading.Tasks.Task<TNewResult> ContinueWith<TNewResult>(Func<System.Threading.Tasks.Task<TResult>,object,TNewResult> continuationFunction, object state, System.Threading.CancellationToken cancellationToken, System.Threading.Tasks.TaskContinuationOptions continuationOptions, System.Threading.Tasks.TaskScheduler scheduler);
override this.ContinueWith : Func<System.Threading.Tasks.Task<'Result>, obj, 'NewResult> * obj * System.Threading.CancellationToken * System.Threading.Tasks.TaskContinuationOptions * System.Threading.Tasks.TaskScheduler -> System.Threading.Tasks.Task<'NewResult>
Public Function ContinueWith(Of TNewResult) (continuationFunction As Func(Of Task(Of TResult), Object, TNewResult), state As Object, cancellationToken As CancellationToken, continuationOptions As TaskContinuationOptions, scheduler As TaskScheduler) As Task(Of TNewResult)

Paramètres de type

TNewResult

Type du résultat produit par la continuation.

Paramètres

continuationFunction
Func<Task<TResult>,Object,TNewResult>

Fonction à exécuter lorsque la Task<TResult> fin est terminée. Lors de l’exécution, le délégué est passé la tâche terminée et l’objet d’état fourni par l’appelant en tant qu’arguments.

state
Object

Objet représentant les données à utiliser par la fonction de continuation.

cancellationToken
CancellationToken

Qui CancellationToken sera affecté à la nouvelle tâche.

continuationOptions
TaskContinuationOptions

Options pour le moment où la continuation est planifiée et comment elle se comporte. Cela inclut des critères, tels que OnlyOnCanceled, ainsi que des options d’exécution, telles que ExecuteSynchronously.

scheduler
TaskScheduler

À TaskScheduler associer à la tâche de continuation et à utiliser pour son exécution.

Retours

Task<TNewResult>

Une nouvelle continuation Task<TResult>.

Exceptions

L’argument scheduler est null.

L’argument continuationOptions spécifie une valeur non valide pour TaskContinuationOptions.

Les données fournies CancellationToken ont déjà été supprimées.

Remarques

Le retour Task<TResult> ne sera pas planifié pour l’exécution tant que la tâche en cours n’est pas terminée, qu’elle soit terminée en raison d’une exécution réussie, d’une erreur en raison d’une exception non gérée ou de la sortie anticipée en raison d’une annulation.

Le continuationFunction, lorsqu’il est exécuté, doit retourner un Task<TResult>. L’état d’achèvement de cette tâche est transféré à la tâche retournée par l’appel Task<TResult>.ContinueWith .

Voir aussi

S’applique à

ContinueWith<TNewResult>(Func<Task<TResult>,TNewResult>, CancellationToken, TaskContinuationOptions, TaskScheduler)

Crée une continuation qui s’exécute en fonction de la condition spécifiée dans continuationOptions.

public:
generic <typename TNewResult>
 System::Threading::Tasks::Task<TNewResult> ^ ContinueWith(Func<System::Threading::Tasks::Task<TResult> ^, TNewResult> ^ continuationFunction, System::Threading::CancellationToken cancellationToken, System::Threading::Tasks::TaskContinuationOptions continuationOptions, System::Threading::Tasks::TaskScheduler ^ scheduler);
public System.Threading.Tasks.Task<TNewResult> ContinueWith<TNewResult>(Func<System.Threading.Tasks.Task<TResult>,TNewResult> continuationFunction, System.Threading.CancellationToken cancellationToken, System.Threading.Tasks.TaskContinuationOptions continuationOptions, System.Threading.Tasks.TaskScheduler scheduler);
override this.ContinueWith : Func<System.Threading.Tasks.Task<'Result>, 'NewResult> * System.Threading.CancellationToken * System.Threading.Tasks.TaskContinuationOptions * System.Threading.Tasks.TaskScheduler -> System.Threading.Tasks.Task<'NewResult>
Public Function ContinueWith(Of TNewResult) (continuationFunction As Func(Of Task(Of TResult), TNewResult), cancellationToken As CancellationToken, continuationOptions As TaskContinuationOptions, scheduler As TaskScheduler) As Task(Of TNewResult)

Paramètres de type

TNewResult

Type du résultat produit par la continuation.

Paramètres

continuationFunction
Func<Task<TResult>,TNewResult>

Fonction à exécuter en fonction de la condition spécifiée dans continuationOptions.

Lors de l’exécution, le délégué est passé en tant qu’argument de cette tâche terminée.

cancellationToken
CancellationToken

Qui CancellationToken sera affecté à la nouvelle tâche.

continuationOptions
TaskContinuationOptions

Options pour le moment où la continuation est planifiée et comment elle se comporte. Cela inclut des critères, tels que OnlyOnCanceled, ainsi que des options d’exécution, telles que ExecuteSynchronously.

scheduler
TaskScheduler

À TaskScheduler associer à la tâche de continuation et à utiliser pour son exécution.

Retours

Task<TNewResult>

Une nouvelle continuation Task<TResult>.

Exceptions

Il Task<TResult> a été supprimé.

-ou-

Celui CancellationTokenSource créé cancellationToken a déjà été supprimé.

L’argument continuationFunction est null.

-ou-

L’argument scheduler est null.

L’argument continuationOptions spécifie une valeur non valide pour TaskContinuationOptions.

Remarques

Le retour Task<TResult> ne sera pas planifié pour l’exécution tant que la tâche en cours n’est pas terminée, qu’elle soit terminée en raison d’une exécution réussie, d’une erreur en raison d’une exception non gérée ou de la sortie anticipée en raison d’une annulation.

Le continuationFunction, lorsqu’il est exécuté, doit retourner un Task<TResult>.

Voir aussi

S’applique à

ContinueWith<TNewResult>(Func<Task<TResult>,Object,TNewResult>, Object, TaskScheduler)

Crée une continuation qui s’exécute une fois la cible Task<TResult> terminée.

public:
generic <typename TNewResult>
 System::Threading::Tasks::Task<TNewResult> ^ ContinueWith(Func<System::Threading::Tasks::Task<TResult> ^, System::Object ^, TNewResult> ^ continuationFunction, System::Object ^ state, System::Threading::Tasks::TaskScheduler ^ scheduler);
public System.Threading.Tasks.Task<TNewResult> ContinueWith<TNewResult>(Func<System.Threading.Tasks.Task<TResult>,object,TNewResult> continuationFunction, object state, System.Threading.Tasks.TaskScheduler scheduler);
override this.ContinueWith : Func<System.Threading.Tasks.Task<'Result>, obj, 'NewResult> * obj * System.Threading.Tasks.TaskScheduler -> System.Threading.Tasks.Task<'NewResult>
Public Function ContinueWith(Of TNewResult) (continuationFunction As Func(Of Task(Of TResult), Object, TNewResult), state As Object, scheduler As TaskScheduler) As Task(Of TNewResult)

Paramètres de type

TNewResult

Type du résultat produit par la continuation.

Paramètres

continuationFunction
Func<Task<TResult>,Object,TNewResult>

Fonction à exécuter lorsque la Task<TResult> fin est terminée. Lors de l’exécution, le délégué est passé la tâche terminée et l’objet d’état fourni par l’appelant en tant qu’arguments.

state
Object

Objet représentant les données à utiliser par la fonction de continuation.

scheduler
TaskScheduler

À TaskScheduler associer à la tâche de continuation et à utiliser pour son exécution.

Retours

Task<TNewResult>

Une nouvelle continuation Task<TResult>.

Exceptions

L’argument scheduler est null.

Remarques

Le retour Task<TResult> ne sera pas planifié pour l’exécution tant que la tâche en cours n’est pas terminée, qu’elle soit terminée en raison d’une exécution réussie, d’une erreur en raison d’une exception non gérée ou de la sortie anticipée en raison d’une annulation.

Voir aussi

S’applique à

ContinueWith<TNewResult>(Func<Task<TResult>,Object,TNewResult>, Object, TaskContinuationOptions)

Crée une continuation qui s’exécute une fois la cible Task<TResult> terminée.

public:
generic <typename TNewResult>
 System::Threading::Tasks::Task<TNewResult> ^ ContinueWith(Func<System::Threading::Tasks::Task<TResult> ^, System::Object ^, TNewResult> ^ continuationFunction, System::Object ^ state, System::Threading::Tasks::TaskContinuationOptions continuationOptions);
public System.Threading.Tasks.Task<TNewResult> ContinueWith<TNewResult>(Func<System.Threading.Tasks.Task<TResult>,object,TNewResult> continuationFunction, object state, System.Threading.Tasks.TaskContinuationOptions continuationOptions);
override this.ContinueWith : Func<System.Threading.Tasks.Task<'Result>, obj, 'NewResult> * obj * System.Threading.Tasks.TaskContinuationOptions -> System.Threading.Tasks.Task<'NewResult>
Public Function ContinueWith(Of TNewResult) (continuationFunction As Func(Of Task(Of TResult), Object, TNewResult), state As Object, continuationOptions As TaskContinuationOptions) As Task(Of TNewResult)

Paramètres de type

TNewResult

Type du résultat produit par la continuation.

Paramètres

continuationFunction
Func<Task<TResult>,Object,TNewResult>

Fonction à exécuter lorsque la Task<TResult> fin est terminée. Lors de l’exécution, le délégué est passé la tâche terminée et l’objet d’état fourni par l’appelant en tant qu’arguments.

state
Object

Objet représentant les données à utiliser par la fonction de continuation.

continuationOptions
TaskContinuationOptions

Options pour le moment où la continuation est planifiée et comment elle se comporte. Cela inclut des critères, tels que OnlyOnCanceled, ainsi que des options d’exécution, telles que ExecuteSynchronously.

Retours

Task<TNewResult>

Une nouvelle continuation Task<TResult>.

Exceptions

L’argument continuationFunction est null.

L’argument continuationOptions spécifie une valeur non valide pour TaskContinuationOptions.

Remarques

Le retour Task<TResult> ne sera pas planifié pour l’exécution tant que la tâche en cours n’est pas terminée, qu’elle soit terminée en raison d’une exécution réussie, d’une erreur en raison d’une exception non gérée ou de la sortie anticipée en raison d’une annulation.

Le continuationFunction, lorsqu’il est exécuté, doit retourner un Task<TResult>. L’état d’achèvement de cette tâche est transféré vers la tâche retournée par l’appel ContinueWith.

Voir aussi

S’applique à

ContinueWith<TNewResult>(Func<Task<TResult>,Object,TNewResult>, Object, CancellationToken)

Crée une continuation qui s’exécute une fois la cible Task<TResult> terminée.

public:
generic <typename TNewResult>
 System::Threading::Tasks::Task<TNewResult> ^ ContinueWith(Func<System::Threading::Tasks::Task<TResult> ^, System::Object ^, TNewResult> ^ continuationFunction, System::Object ^ state, System::Threading::CancellationToken cancellationToken);
public System.Threading.Tasks.Task<TNewResult> ContinueWith<TNewResult>(Func<System.Threading.Tasks.Task<TResult>,object,TNewResult> continuationFunction, object state, System.Threading.CancellationToken cancellationToken);
override this.ContinueWith : Func<System.Threading.Tasks.Task<'Result>, obj, 'NewResult> * obj * System.Threading.CancellationToken -> System.Threading.Tasks.Task<'NewResult>
Public Function ContinueWith(Of TNewResult) (continuationFunction As Func(Of Task(Of TResult), Object, TNewResult), state As Object, cancellationToken As CancellationToken) As Task(Of TNewResult)

Paramètres de type

TNewResult

Type du résultat produit par la continuation.

Paramètres

continuationFunction
Func<Task<TResult>,Object,TNewResult>

Fonction à exécuter lorsque la Task<TResult> fin est terminée. Lors de l’exécution, le délégué est passé la tâche terminée et l’objet d’état fourni par l’appelant en tant qu’arguments.

state
Object

Objet représentant les données à utiliser par la fonction de continuation.

cancellationToken
CancellationToken

Qui CancellationToken sera affecté à la nouvelle tâche.

Retours

Task<TNewResult>

Une nouvelle continuation Task<TResult>.

Exceptions

L’argument continuationFunction est null.

Les données fournies CancellationToken ont déjà été supprimées.

Remarques

Le retour Task<TResult> ne sera pas planifié pour l’exécution tant que la tâche en cours n’est pas terminée, qu’elle soit terminée en raison d’une exécution réussie, d’une erreur en raison d’une exception non gérée ou de la sortie anticipée en raison d’une annulation.

Voir aussi

S’applique à

ContinueWith<TNewResult>(Func<Task<TResult>,TNewResult>, TaskContinuationOptions)

Crée une continuation qui s’exécute en fonction de la condition spécifiée dans continuationOptions.

public:
generic <typename TNewResult>
 System::Threading::Tasks::Task<TNewResult> ^ ContinueWith(Func<System::Threading::Tasks::Task<TResult> ^, TNewResult> ^ continuationFunction, System::Threading::Tasks::TaskContinuationOptions continuationOptions);
public System.Threading.Tasks.Task<TNewResult> ContinueWith<TNewResult>(Func<System.Threading.Tasks.Task<TResult>,TNewResult> continuationFunction, System.Threading.Tasks.TaskContinuationOptions continuationOptions);
override this.ContinueWith : Func<System.Threading.Tasks.Task<'Result>, 'NewResult> * System.Threading.Tasks.TaskContinuationOptions -> System.Threading.Tasks.Task<'NewResult>
Public Function ContinueWith(Of TNewResult) (continuationFunction As Func(Of Task(Of TResult), TNewResult), continuationOptions As TaskContinuationOptions) As Task(Of TNewResult)

Paramètres de type

TNewResult

Type du résultat produit par la continuation.

Paramètres

continuationFunction
Func<Task<TResult>,TNewResult>

Fonction à exécuter en fonction de la condition spécifiée dans continuationOptions.

Lors de l’exécution, le délégué est passé la tâche terminée en tant qu’argument.

continuationOptions
TaskContinuationOptions

Options pour le moment où la continuation est planifiée et comment elle se comporte. Cela inclut des critères, tels que OnlyOnCanceled, ainsi que des options d’exécution, telles que ExecuteSynchronously.

Retours

Task<TNewResult>

Une nouvelle continuation Task<TResult>.

Exceptions

Il Task<TResult> a été supprimé.

L’argument continuationFunction est null.

L’argument continuationOptions spécifie une valeur non valide pour TaskContinuationOptions.

Remarques

Le retour Task<TResult> ne sera pas planifié pour l’exécution tant que la tâche en cours n’est pas terminée, qu’elle soit terminée en raison d’une exécution réussie, d’une erreur en raison d’une exception non gérée ou de la sortie anticipée en raison d’une annulation.

Le continuationFunction, lorsqu’il est exécuté, doit retourner un Task<TResult>.

Voir aussi

S’applique à

ContinueWith<TNewResult>(Func<Task<TResult>,TNewResult>, CancellationToken)

Crée une continuation qui s’exécute de manière asynchrone une fois la cible Task<TResult> terminée.

public:
generic <typename TNewResult>
 System::Threading::Tasks::Task<TNewResult> ^ ContinueWith(Func<System::Threading::Tasks::Task<TResult> ^, TNewResult> ^ continuationFunction, System::Threading::CancellationToken cancellationToken);
public System.Threading.Tasks.Task<TNewResult> ContinueWith<TNewResult>(Func<System.Threading.Tasks.Task<TResult>,TNewResult> continuationFunction, System.Threading.CancellationToken cancellationToken);
override this.ContinueWith : Func<System.Threading.Tasks.Task<'Result>, 'NewResult> * System.Threading.CancellationToken -> System.Threading.Tasks.Task<'NewResult>
Public Function ContinueWith(Of TNewResult) (continuationFunction As Func(Of Task(Of TResult), TNewResult), cancellationToken As CancellationToken) As Task(Of TNewResult)

Paramètres de type

TNewResult

Type du résultat produit par la continuation.

Paramètres

continuationFunction
Func<Task<TResult>,TNewResult>

Fonction à exécuter lorsque la Task<TResult> fin est terminée. Lors de l’exécution, le délégué est passé la tâche terminée en tant qu’argument.

cancellationToken
CancellationToken

Qui CancellationToken sera affecté à la nouvelle tâche.

Retours

Task<TNewResult>

Une nouvelle continuation Task<TResult>.

Exceptions

Il Task<TResult> a été supprimé.

-ou-

Celui CancellationTokenSource créé cancellationToken a déjà été supprimé.

L’argument continuationFunction est null.

Remarques

Le retour Task<TResult> ne sera pas planifié pour l’exécution tant que la tâche en cours n’est pas terminée, qu’elle soit terminée en raison d’une exécution réussie, d’une erreur en raison d’une exception non gérée ou de la sortie anticipée en raison d’une annulation.

Voir aussi

S’applique à

ContinueWith<TNewResult>(Func<Task<TResult>,Object,TNewResult>, Object)

Crée une continuation qui s’exécute une fois la cible Task<TResult> terminée.

public:
generic <typename TNewResult>
 System::Threading::Tasks::Task<TNewResult> ^ ContinueWith(Func<System::Threading::Tasks::Task<TResult> ^, System::Object ^, TNewResult> ^ continuationFunction, System::Object ^ state);
public System.Threading.Tasks.Task<TNewResult> ContinueWith<TNewResult>(Func<System.Threading.Tasks.Task<TResult>,object,TNewResult> continuationFunction, object state);
override this.ContinueWith : Func<System.Threading.Tasks.Task<'Result>, obj, 'NewResult> * obj -> System.Threading.Tasks.Task<'NewResult>
Public Function ContinueWith(Of TNewResult) (continuationFunction As Func(Of Task(Of TResult), Object, TNewResult), state As Object) As Task(Of TNewResult)

Paramètres de type

TNewResult

Type du résultat produit par la continuation.

Paramètres

continuationFunction
Func<Task<TResult>,Object,TNewResult>

Fonction à exécuter lorsque la Task<TResult> fin est terminée. Lors de l’exécution, le délégué est passé la tâche terminée et l’objet d’état fourni par l’appelant en tant qu’arguments.

state
Object

Objet représentant les données à utiliser par la fonction de continuation.

Retours

Task<TNewResult>

Une nouvelle continuation Task<TResult>.

Exceptions

L’argument continuationFunction est null.

Exemples

L’exemple suivant crée une chaîne de tâches de continuation. Chaque tâche fournit l’heure actuelle, un DateTime objet, pour l’argument d’état de la ContinueWith(Action<Task,Object>, Object) méthode. Chaque DateTime valeur représente l’heure à laquelle la tâche continue est créée. Chaque tâche produit comme résultat une deuxième DateTime valeur qui représente l’heure à laquelle la tâche se termine. Une fois toutes les tâches terminées, l’exemple affiche la date et l’heure à laquelle chaque tâche de continuation démarre et se termine.

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

// Demonstrates how to associate state with task continuations.
class ContinuationState
{
   // Simluates a lengthy operation and returns the time at which
   // the operation completed.
   public static DateTime DoWork()
   {
      // Simulate work by suspending the current thread
      // for two seconds.
      Thread.Sleep(2000);

      // Return the current time.
      return DateTime.Now;
   }

   static void Main(string[] args)
   {
      // Start a root task that performs work.
      Task<DateTime> t = Task<DateTime>.Run(delegate { return DoWork(); });

      // Create a chain of continuation tasks, where each task is
      // followed by another task that performs work.
      List<Task<DateTime>> continuations = new List<Task<DateTime>>();
      for (int i = 0; i < 5; i++)
      {
         // Provide the current time as the state of the continuation.
         t = t.ContinueWith(delegate { return DoWork(); }, DateTime.Now);
         continuations.Add(t);
      }

      // Wait for the last task in the chain to complete.
      t.Wait();

      // Print the creation time of each continuation (the state object)
      // and the completion time (the result of that task) to the console.
      foreach (var continuation in continuations)
      {
         DateTime start = (DateTime)continuation.AsyncState;
         DateTime end = continuation.Result;

         Console.WriteLine("Task was created at {0} and finished at {1}.",
            start.TimeOfDay, end.TimeOfDay);
      }
   }
}

/* Sample output:
Task was created at 10:56:21.1561762 and finished at 10:56:25.1672062.
Task was created at 10:56:21.1610677 and finished at 10:56:27.1707646.
Task was created at 10:56:21.1610677 and finished at 10:56:29.1743230.
Task was created at 10:56:21.1610677 and finished at 10:56:31.1779883.
Task was created at 10:56:21.1610677 and finished at 10:56:33.1837083.
*/
Imports System.Collections.Generic
Imports System.Threading
Imports System.Threading.Tasks

' Demonstrates how to associate state with task continuations.
Public Module ContinuationState
   ' Simluates a lengthy operation and returns the time at which
   ' the operation completed.
   Public Function DoWork() As Date
      ' Simulate work by suspending the current thread 
      ' for two seconds.
      Thread.Sleep(2000)

      ' Return the current time.
      Return Date.Now
   End Function

   Public Sub Main()
      ' Start a root task that performs work.
      Dim t As Task(Of Date) = Task(Of Date).Run(Function() DoWork())

      ' Create a chain of continuation tasks, where each task is
      ' followed by another task that performs work.
      Dim continuations As New List(Of Task(Of DateTime))()
      For i As Integer = 0 To 4
         ' Provide the current time as the state of the continuation.
         t = t.ContinueWith(Function(antecedent, state) DoWork(), DateTime.Now)
         continuations.Add(t)
      Next

      ' Wait for the last task in the chain to complete.
      t.Wait()

      ' Display the creation time of each continuation (the state object)
      ' and the completion time (the result of that task) to the console.
      For Each continuation In continuations
         Dim start As DateTime = CDate(continuation.AsyncState)
         Dim [end] As DateTime = continuation.Result

         Console.WriteLine("Task was created at {0} and finished at {1}.",
            start.TimeOfDay, [end].TimeOfDay)
      Next
   End Sub
End Module
' The example displays output like the following:
'       Task was created at 10:56:21.1561762 and finished at 10:56:25.1672062.
'       Task was created at 10:56:21.1610677 and finished at 10:56:27.1707646.
'       Task was created at 10:56:21.1610677 and finished at 10:56:29.1743230.
'       Task was created at 10:56:21.1610677 and finished at 10:56:31.1779883.
'       Task was created at 10:56:21.1610677 and finished at 10:56:33.1837083.

Remarques

Le retour Task<TResult> ne sera pas planifié pour l’exécution tant que la tâche en cours n’est pas terminée, qu’elle soit terminée en raison d’une exécution réussie, d’une erreur en raison d’une exception non gérée ou de la sortie anticipée en raison d’une annulation.

Voir aussi

S’applique à

ContinueWith<TNewResult>(Func<Task<TResult>,TNewResult>)

Crée une continuation qui s’exécute de manière asynchrone une fois la cible Task<TResult> terminée.

public:
generic <typename TNewResult>
 System::Threading::Tasks::Task<TNewResult> ^ ContinueWith(Func<System::Threading::Tasks::Task<TResult> ^, TNewResult> ^ continuationFunction);
public System.Threading.Tasks.Task<TNewResult> ContinueWith<TNewResult>(Func<System.Threading.Tasks.Task<TResult>,TNewResult> continuationFunction);
override this.ContinueWith : Func<System.Threading.Tasks.Task<'Result>, 'NewResult> -> System.Threading.Tasks.Task<'NewResult>
Public Function ContinueWith(Of TNewResult) (continuationFunction As Func(Of Task(Of TResult), TNewResult)) As Task(Of TNewResult)

Paramètres de type

TNewResult

Type du résultat produit par la continuation.

Paramètres

continuationFunction
Func<Task<TResult>,TNewResult>

Fonction à exécuter lorsque la Task<TResult> fin est terminée. Lors de l’exécution, le délégué est passé la tâche terminée en tant qu’argument.

Retours

Task<TNewResult>

Une nouvelle continuation Task<TResult>.

Exceptions

Il Task<TResult> a été supprimé.

L’argument continuationFunction est null.

Remarques

Le retour Task<TResult> ne sera pas planifié pour l’exécution tant que la tâche en cours n’est pas terminée, qu’elle soit terminée en raison d’une exécution réussie, d’une erreur en raison d’une exception non gérée ou de la sortie anticipée en raison d’une annulation.

Voir aussi

S’applique à

ContinueWith<TNewResult>(Func<Task<TResult>,TNewResult>, TaskScheduler)

Crée une continuation qui s’exécute de manière asynchrone une fois la cible Task<TResult> terminée.

public:
generic <typename TNewResult>
 System::Threading::Tasks::Task<TNewResult> ^ ContinueWith(Func<System::Threading::Tasks::Task<TResult> ^, TNewResult> ^ continuationFunction, System::Threading::Tasks::TaskScheduler ^ scheduler);
public System.Threading.Tasks.Task<TNewResult> ContinueWith<TNewResult>(Func<System.Threading.Tasks.Task<TResult>,TNewResult> continuationFunction, System.Threading.Tasks.TaskScheduler scheduler);
override this.ContinueWith : Func<System.Threading.Tasks.Task<'Result>, 'NewResult> * System.Threading.Tasks.TaskScheduler -> System.Threading.Tasks.Task<'NewResult>
Public Function ContinueWith(Of TNewResult) (continuationFunction As Func(Of Task(Of TResult), TNewResult), scheduler As TaskScheduler) As Task(Of TNewResult)

Paramètres de type

TNewResult

Type du résultat produit par la continuation.

Paramètres

continuationFunction
Func<Task<TResult>,TNewResult>

Fonction à exécuter lorsque la Task<TResult> fin est terminée. Lors de l’exécution, le délégué est passé la tâche terminée en tant qu’argument.

scheduler
TaskScheduler

À TaskScheduler associer à la tâche de continuation et à utiliser pour son exécution.

Retours

Task<TNewResult>

Une nouvelle continuation Task<TResult>.

Exceptions

Il Task<TResult> a été supprimé.

L’argument continuationFunction est null.

-ou-

L’argument scheduler est null.

Remarques

Le retour Task<TResult> ne sera pas planifié pour l’exécution tant que la tâche en cours n’est pas terminée, qu’elle soit terminée en raison d’une exécution réussie, d’une erreur en raison d’une exception non gérée ou de la sortie anticipée en raison d’une annulation.

Voir aussi

S’applique à