CKTIS / C#
System.Threading.Tasks.TaskCompletionSource: praktická reference
System.Threading.Tasks.TaskCompletionSource: praktická reference: TaskCompletionSource<T> umožňuje producentovi ručně dokončit Task<T>.
Verze: .NET API; verified against .NET 10 reference surface
Syntaxe
System.Threading.Tasks.TaskCompletionSource /* select a documented member or constructor */TaskCompletionSource umožňuje producentovi ručně dokončit Task.
Použijte k převodu callbacků a událostí na model task.
Preferujte RunContinuationsAsynchronously, aby callback neprováděl těžkou práci uvnitř SetResult.
Druhé Set* vyhodí výjimku; TrySet* vrátí false, pokud už došlo k dokončení.
Úplný program skutečně používá System.Threading.Tasks.TaskCompletionSource; přesně očekávaný stdout je 9.
Příklady
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);
Výsledek
9
Úplný program skutečně používá `System.Threading.Tasks.TaskCompletionSource`; přesně očekávaný stdout je `9`.