<PackageReference Include="System.IO.Pipelines" Version="10.0.0-rc.2.25502.107" />

TaskToAsyncResult

static class TaskToAsyncResult
using System.Runtime.CompilerServices; namespace System.Threading.Tasks { [System.Runtime.CompilerServices.NullableContext(1)] [System.Runtime.CompilerServices.Nullable(0)] internal static class TaskToAsyncResult { private sealed class TaskAsyncResult : IAsyncResult { internal readonly Task _task; private readonly AsyncCallback _callback; public object AsyncState { get; } public bool CompletedSynchronously { get; } public bool IsCompleted => _task.IsCompleted; public WaitHandle AsyncWaitHandle => ((IAsyncResult)_task).AsyncWaitHandle; internal TaskAsyncResult(Task task, object state, AsyncCallback callback) { _task = task; AsyncState = state; if (task.IsCompleted) { CompletedSynchronously = true; callback?.Invoke(this); } else if (callback != null) { _callback = callback; _task.ConfigureAwait(false).GetAwaiter().OnCompleted(delegate { _callback(this); }); } } } public static IAsyncResult Begin(Task task, [System.Runtime.CompilerServices.Nullable(2)] AsyncCallback callback, [System.Runtime.CompilerServices.Nullable(2)] object state) { ExceptionPolyfills.ThrowIfNull(task, "task"); return new TaskAsyncResult(task, state, callback); } public static void End(IAsyncResult asyncResult) { Unwrap(asyncResult).GetAwaiter().GetResult(); } public static TResult End<[System.Runtime.CompilerServices.Nullable(2)] TResult>(IAsyncResult asyncResult) { return Unwrap<TResult>(asyncResult).GetAwaiter().GetResult(); } public static Task Unwrap(IAsyncResult asyncResult) { ExceptionPolyfills.ThrowIfNull(asyncResult, "asyncResult"); Task obj = (asyncResult as TaskAsyncResult)?._task; if (obj == null) throw new ArgumentException(null, "asyncResult"); return obj; } public static Task<TResult> Unwrap<[System.Runtime.CompilerServices.Nullable(2)] TResult>(IAsyncResult asyncResult) { ExceptionPolyfills.ThrowIfNull(asyncResult, "asyncResult"); Task<TResult> obj = (asyncResult as TaskAsyncResult)?._task as Task<TResult>; if (obj == null) throw new ArgumentException(null, "asyncResult"); return obj; } } }