CKTIS / C#
System.Threading.CancellationToken: praktična referenca
System.Threading.CancellationToken: praktična referenca: CancellationToken prenaša sodelovalni signal preklica brez prisilne prekinitve niti.
Različica: .NET API; verified against .NET 10 reference surface
Sintaksa
System.Threading.CancellationToken /* select a documented member or constructor */CancellationToken prenaša sodelovalni signal preklica brez prisilne prekinitve niti.
Podajajte ga skozi celoten sklad async ter ga redno preverjajte ali registrirajte callback.
Preklic ne zagotavlja takojšnjega konca; callback se lahko izvede sinhrono.
None se nikoli ne prekliče; ThrowIfCancellationRequested sproži s pravilnim tokenom.
Celoten program dejansko uporabi System.Threading.CancellationToken; natančno pričakovani stdout je True.
Primeri
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);
Rezultat
True
Celoten program dejansko uporabi `System.Threading.CancellationToken`; natančno pričakovani stdout je `True`.