CKTIS / C#
System.Threading.Tasks.TaskCompletionSource: praktická referencia
System.Threading.Tasks.TaskCompletionSource: praktická referencia: TaskCompletionSource<T> umožňuje producentovi ručne dokončiť Task<T>.
Verzia: .NET API; verified against .NET 10 reference surface
Syntax
System.Threading.Tasks.TaskCompletionSource /* select a documented member or constructor */TaskCompletionSource umožňuje producentovi ručne dokončiť Task.
Použite na prevod callbackov a udalostí na model task.
Preferujte RunContinuationsAsynchronously, aby callback nevykonával ťažkú prácu vnútri SetResult.
Druhé Set* vyhodí výnimku; TrySet* vráti false, ak už došlo k dokončeniu.
Úplný program skutočne používa System.Threading.Tasks.TaskCompletionSource; presne očakávaný stdout je 9.
Prí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ýsledok
9
Úplný program skutočne používa `System.Threading.Tasks.TaskCompletionSource`; presne očakávaný stdout je `9`.