DotNext by Roman Sakno

<PackageReference Include="DotNext" Version="2.4.2" />

 Synchronization

public static class Synchronization
Represents task synchronization and combination methods.
using DotNext.Generic; using System; using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; namespace DotNext.Threading.Tasks { [System.Runtime.CompilerServices.NullableContext(1)] [System.Runtime.CompilerServices.Nullable(0)] public static class Synchronization { private static readonly Func<Task, bool> TrueContinuation = (Task task) => true; [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] public static Result<TResult> GetResult<[System.Runtime.CompilerServices.Nullable(2)] TResult>(this Task<TResult> task, TimeSpan timeout) { try { return task.Wait(timeout) ? ((Result<TResult>)task.Result) : new Result<TResult>((Exception)new TimeoutException()); } catch (Exception error) { return new Result<TResult>(error); } } [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] public static Result<TResult> GetResult<[System.Runtime.CompilerServices.Nullable(2)] TResult>(this Task<TResult> task, CancellationToken token) { try { task.Wait(token); return task.Result; } catch (Exception error) { return new Result<TResult>(error); } } [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] public static Result<dynamic> GetResult(this Task task, CancellationToken token) { try { task.Wait(token); object source = DynamicTaskAwaitable.GetResult(task); return new Result<object>(Unsafe.As<object, object>(ref source)); } catch (Exception error) { return new Result<object>(error); } } [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] public static Result<dynamic> GetResult(this Task task, TimeSpan timeout) { try { if (!task.Wait(timeout)) return new Result<object>(new TimeoutException()); object source = DynamicTaskAwaitable.GetResult(task); return new Result<object>(Unsafe.As<object, object>(ref source)); } catch (Exception error) { return new Result<object>(error); } } [AsyncStateMachine(typeof(<WhenAll>d__5<, >))] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 1, 0, 1, 1 })] public static Task<(T1, T2)> WhenAll<[System.Runtime.CompilerServices.Nullable(2)] T1, [System.Runtime.CompilerServices.Nullable(2)] T2>(Task<T1> task1, Task<T2> task2) { <WhenAll>d__5<T1, T2> stateMachine = default(<WhenAll>d__5<T1, T2>); stateMachine.task1 = task1; stateMachine.task2 = task2; stateMachine.<>t__builder = AsyncTaskMethodBuilder<(T1, T2)>.Create(); stateMachine.<>1__state = -1; stateMachine.<>t__builder.Start(ref stateMachine); return stateMachine.<>t__builder.Task; } [AsyncStateMachine(typeof(<WhenAll>d__6<, , >))] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 1, 0, 1, 1, 1 })] public static Task<(T1, T2, T3)> WhenAll<[System.Runtime.CompilerServices.Nullable(2)] T1, [System.Runtime.CompilerServices.Nullable(2)] T2, [System.Runtime.CompilerServices.Nullable(2)] T3>(Task<T1> task1, Task<T2> task2, Task<T3> task3) { <WhenAll>d__6<T1, T2, T3> stateMachine = default(<WhenAll>d__6<T1, T2, T3>); stateMachine.task1 = task1; stateMachine.task2 = task2; stateMachine.task3 = task3; stateMachine.<>t__builder = AsyncTaskMethodBuilder<(T1, T2, T3)>.Create(); stateMachine.<>1__state = -1; stateMachine.<>t__builder.Start(ref stateMachine); return stateMachine.<>t__builder.Task; } [AsyncStateMachine(typeof(<WhenAll>d__7<, , , >))] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 1, 0, 1, 1, 1, 1 })] public static Task<(T1, T2, T3, T4)> WhenAll<[System.Runtime.CompilerServices.Nullable(2)] T1, [System.Runtime.CompilerServices.Nullable(2)] T2, [System.Runtime.CompilerServices.Nullable(2)] T3, [System.Runtime.CompilerServices.Nullable(2)] T4>(Task<T1> task1, Task<T2> task2, Task<T3> task3, Task<T4> task4) { <WhenAll>d__7<T1, T2, T3, T4> stateMachine = default(<WhenAll>d__7<T1, T2, T3, T4>); stateMachine.task1 = task1; stateMachine.task2 = task2; stateMachine.task3 = task3; stateMachine.task4 = task4; stateMachine.<>t__builder = AsyncTaskMethodBuilder<(T1, T2, T3, T4)>.Create(); stateMachine.<>1__state = -1; stateMachine.<>t__builder.Start(ref stateMachine); return stateMachine.<>t__builder.Task; } [AsyncStateMachine(typeof(<WhenAll>d__8<, , , , >))] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 1, 0, 1, 1, 1, 1, 1 })] public static Task<(T1, T2, T3, T4, T5)> WhenAll<[System.Runtime.CompilerServices.Nullable(2)] T1, [System.Runtime.CompilerServices.Nullable(2)] T2, [System.Runtime.CompilerServices.Nullable(2)] T3, [System.Runtime.CompilerServices.Nullable(2)] T4, [System.Runtime.CompilerServices.Nullable(2)] T5>(Task<T1> task1, Task<T2> task2, Task<T3> task3, Task<T4> task4, Task<T5> task5) { <WhenAll>d__8<T1, T2, T3, T4, T5> stateMachine = default(<WhenAll>d__8<T1, T2, T3, T4, T5>); stateMachine.task1 = task1; stateMachine.task2 = task2; stateMachine.task3 = task3; stateMachine.task4 = task4; stateMachine.task5 = task5; stateMachine.<>t__builder = AsyncTaskMethodBuilder<(T1, T2, T3, T4, T5)>.Create(); stateMachine.<>1__state = -1; stateMachine.<>t__builder.Start(ref stateMachine); return stateMachine.<>t__builder.Task; } [AsyncStateMachine(typeof(<WaitAsyncImpl>d__9))] private static Task<bool> WaitAsyncImpl(Task task, TimeSpan timeout, CancellationToken token) { <WaitAsyncImpl>d__9 stateMachine = default(<WaitAsyncImpl>d__9); stateMachine.task = task; stateMachine.timeout = timeout; stateMachine.token = token; stateMachine.<>t__builder = AsyncTaskMethodBuilder<bool>.Create(); stateMachine.<>1__state = -1; stateMachine.<>t__builder.Start(ref stateMachine); return stateMachine.<>t__builder.Task; } public static Task<bool> WaitAsync(this Task task, TimeSpan timeout, CancellationToken token = default(CancellationToken)) { if (timeout < TimeSpan.Zero && timeout != System.Threading.Timeout.InfiniteTimeSpan) throw new ArgumentOutOfRangeException("timeout"); if (token.IsCancellationRequested) return Task.FromCanceled<bool>(token); if (task.IsCompleted) return CompletedTask<bool, BooleanConst.True>.Task; if (timeout == TimeSpan.Zero) return CompletedTask<bool, BooleanConst.False>.Task; if (timeout > System.Threading.Timeout.InfiniteTimeSpan) return WaitAsyncImpl(task, timeout, token); if (!token.CanBeCanceled) { Task<bool> task2 = task as Task<bool>; if (task2 != null) return task2; } return task.ContinueWith(TrueContinuation, token, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Current); } } }