Base di conoscenzaCentro esami
ITItaliano
Scegli la lingua13

CKTIS / C#

System.Threading.CancellationToken: riferimento pratico

System.Threading.CancellationToken: riferimento pratico: CancellationToken trasporta un segnale di annullamento cooperativo senza interrompere forzatamente un thread.

Versione: .NET API; verified against .NET 10 reference surface

Sintassi

System.Threading.CancellationToken /* select a documented member or constructor */

CancellationToken trasporta un segnale di annullamento cooperativo senza interrompere forzatamente un thread.

Passalo lungo tutto lo stack async e controllalo o registra periodicamente un callback.

L’annullamento non garantisce la fine immediata; un callback può essere eseguito in modo sincrono.

None non viene mai annullato; ThrowIfCancellationRequested genera con il token corretto.

Il programma completo usa realmente System.Threading.CancellationToken; lo stdout esatto atteso è True.

Esempi

using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using Number = System.Int32;

using var source = new CancellationTokenSource();
CancellationToken token = source.Token; source.Cancel();
Console.WriteLine(token.IsCancellationRequested);

Risultato

True

Il programma completo usa realmente `System.Threading.CancellationToken`; lo stdout esatto atteso è `True`.

Fonti