CancellationToken.IsCancellationRequested Proprietà

Definizione

Ottiene un valore che indica se per questo token è stato richiesto l'annullamento.

C#
public bool IsCancellationRequested { get; }

Valore della proprietà

true se per questo token è stato richiesto l'annullamento; in caso contrario false.

Esempio

Di seguito è riportato un semplice esempio che esegue un processo server fino a quando la IsCancellationRequested proprietà non restituisce true.

C#
using System;
using System.Threading;

public class ServerClass
{
   public static void StaticMethod(object obj)
   {
      CancellationToken ct = (CancellationToken)obj;
      Console.WriteLine("ServerClass.StaticMethod is running on another thread.");

      // Simulate work that can be canceled.
      while (!ct.IsCancellationRequested) {
         Thread.SpinWait(50000);
      }
      Console.WriteLine("The worker thread has been canceled. Press any key to exit.");
      Console.ReadKey(true);
   }
}

public class Simple
{
   public static void Main()
   {
      // The Simple class controls access to the token source.
      CancellationTokenSource cts = new CancellationTokenSource();

      Console.WriteLine("Press 'C' to terminate the application...\n");
      // Allow the UI thread to capture the token source, so that it
      // can issue the cancel command.
      Thread t1 = new Thread(() => { if (Console.ReadKey(true).KeyChar.ToString().ToUpperInvariant() == "C")
                                     cts.Cancel(); } );

      // ServerClass sees only the token, not the token source.
      Thread t2 = new Thread(new ParameterizedThreadStart(ServerClass.StaticMethod));
      // Start the UI thread.

      t1.Start();

      // Start the worker thread and pass it the token.
      t2.Start(cts.Token);

      t2.Join();
      cts.Dispose();
   }
}
// The example displays the following output:
//       Press 'C' to terminate the application...
//
//       ServerClass.StaticMethod is running on another thread.
//       The worker thread has been canceled. Press any key to exit.

Nell'esempio viene creata un'istanza di un CancellationTokenSource oggetto , che controlla l'accesso al token di annullamento. Definisce quindi due procedure di thread. Il primo viene definito come espressione lambda che esegue il pool della tastiera e, quando viene premuto il tasto "C", chiama CancellationTokenSource.Cancel per impostare il token di annullamento sullo stato annullato. Il secondo è un metodo con parametri, ServerClass.StaticMethod, che esegue un ciclo fino a quando la IsCancellationRequested proprietà non è true.

Il thread principale avvia quindi i due thread e si blocca finché il thread che esegue il ServerClass.StaticMethod metodo termina.

Commenti

Questa proprietà indica se l'annullamento è stato richiesto per questo token, tramite il token inizialmente costruito in uno stato annullato o tramite la chiamata Cancel all'oggetto associato CancellationTokenSourceal token.

Se questa proprietà è true, garantisce solo che sia stato richiesto l'annullamento. Non garantisce che ogni gestore registrato abbia terminato l'esecuzione, né che le richieste di annullamento siano state propagate a tutti i gestori registrati. Potrebbe essere necessaria una sincronizzazione aggiuntiva, in particolare nelle situazioni in cui gli oggetti correlati vengono annullati contemporaneamente.

Si applica a

Prodotto Versioni
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Vedi anche