Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

System.Threading.CancellationToken: practical reference

System.Threading.CancellationToken: practical reference: CancellationToken carries a cooperative cancellation signal without forcibly aborting a thread.

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

Syntax

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

CancellationToken carries a cooperative cancellation signal without forcibly aborting a thread.

Pass it through the entire async stack and check it or register a callback periodically.

Cancellation does not guarantee immediate termination; a callback can run synchronously.

None is never canceled; ThrowIfCancellationRequested throws with the proper token.

The complete program actually uses System.Threading.CancellationToken; its exact expected stdout is True.

Examples

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);

Result

True

The complete program actually uses `System.Threading.CancellationToken`; its exact expected stdout is `True`.

Sources