CKTIS / C#
System.Threading.Tasks.TaskCompletionSource: riferimento pratico
System.Threading.Tasks.TaskCompletionSource: riferimento pratico: TaskCompletionSource<T> consente a un produttore di completare manualmente un Task<T>.
Versione: .NET API; verified against .NET 10 reference surface
Sintassi
System.Threading.Tasks.TaskCompletionSource /* select a documented member or constructor */TaskCompletionSource consente a un produttore di completare manualmente un Task.
Usalo per adattare callback ed eventi al modello task.
Preferisci RunContinuationsAsynchronously affinché un callback non svolga lavoro pesante dentro SetResult.
Un secondo Set* genera un’eccezione; TrySet* restituisce false se il completamento è già avvenuto.
Il programma completo usa realmente System.Threading.Tasks.TaskCompletionSource; lo stdout esatto atteso è 9.
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;
var source = new TaskCompletionSource<int>(TaskCreationOptions.RunContinuationsAsynchronously);
source.SetResult(9);
Console.WriteLine(await source.Task);
Risultato
9
Il programma completo usa realmente `System.Threading.Tasks.TaskCompletionSource`; lo stdout esatto atteso è `9`.