DotNext by .NET Foundation and Contributors

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

 Synchronization

public static class Synchronization
Represents task synchronization methods.
using System; using System.Diagnostics.CodeAnalysis; 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 { [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) ? new Result<TResult>(task.Result) : new Result<TResult>((Exception)new TimeoutException()); } catch (AggregateException ex) when (ex.InnerExceptions.Count == 1) { return new Result<TResult>(ex.InnerExceptions[0]); } 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 (AggregateException ex) when (ex.InnerExceptions.Count == 1) { return new Result<TResult>(ex.InnerExceptions[0]); } catch (Exception error) { return new Result<TResult>(error); } } [System.Runtime.CompilerServices.NullableContext(2)] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] public static Result<TResult>? TryGetResult<TResult>([System.Runtime.CompilerServices.Nullable(new byte[] { 2, 1 })] this Task<TResult> task) { if (task == null || !task.IsCompleted) return null; return task.GetResult(CancellationToken.None); } [RequiresUnreferencedCode("Runtime binding may be incompatible with IL trimming")] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 2 })] public static Result<dynamic> GetResult(this Task task, CancellationToken token) { try { task.Wait(token); return new Result<object>(new DynamicTaskAwaitable.Awaiter(task, false).GetRawResult()); } catch (AggregateException ex) when (ex.InnerExceptions.Count == 1) { return new Result<object>(ex.InnerExceptions[0]); } catch (Exception error) { return new Result<object>(error); } } [RequiresUnreferencedCode("Runtime binding may be incompatible with IL trimming")] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 2 })] public static Result<dynamic> GetResult(this Task task, TimeSpan timeout) { try { if (!task.Wait(timeout)) throw new TimeoutException(); return new Result<object>(new DynamicTaskAwaitable.Awaiter(task, false).GetRawResult()); } catch (AggregateException ex) when (ex.InnerExceptions.Count == 1) { return new Result<object>(ex.InnerExceptions[0]); } catch (Exception error) { return new Result<object>(error); } } [AsyncStateMachine(typeof(<WhenAll>d__5<, >))] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 1, 0, 0, 1, 0, 1 })] public static Task<(Result<T1>, Result<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.<>t__builder = AsyncTaskMethodBuilder<(Result<T1>, Result<T2>)>.Create(); stateMachine.task1 = task1; stateMachine.task2 = task2; 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, 0, 1, 0, 1, 0, 1 })] public static Task<(Result<T1>, Result<T2>, Result<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.<>t__builder = AsyncTaskMethodBuilder<(Result<T1>, Result<T2>, Result<T3>)>.Create(); stateMachine.task1 = task1; stateMachine.task2 = task2; stateMachine.task3 = task3; 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, 0, 1, 0, 1, 0, 1, 0, 1 })] public static Task<(Result<T1>, Result<T2>, Result<T3>, Result<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.<>t__builder = AsyncTaskMethodBuilder<(Result<T1>, Result<T2>, Result<T3>, Result<T4>)>.Create(); stateMachine.task1 = task1; stateMachine.task2 = task2; stateMachine.task3 = task3; stateMachine.task4 = task4; 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, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1 })] public static Task<(Result<T1>, Result<T2>, Result<T3>, Result<T4>, Result<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.<>t__builder = AsyncTaskMethodBuilder<(Result<T1>, Result<T2>, Result<T3>, Result<T4>, Result<T5>)>.Create(); stateMachine.task1 = task1; stateMachine.task2 = task2; stateMachine.task3 = task3; stateMachine.task4 = task4; stateMachine.task5 = task5; stateMachine.<>1__state = -1; stateMachine.<>t__builder.Start(ref stateMachine); return stateMachine.<>t__builder.Task; } [AsyncStateMachine(typeof(<WhenAll>d__9<>))] [AsyncMethodBuilder(typeof(PoolingAsyncValueTaskMethodBuilder))] private static ValueTask WhenAll<T>(T tasks) where T : struct, ITuple { <WhenAll>d__9<T> <WhenAll>d__ = default(<WhenAll>d__9<T>); <WhenAll>d__.<>t__builder = PoolingAsyncValueTaskMethodBuilder.Create(); <WhenAll>d__.tasks = tasks; <WhenAll>d__.<>1__state = -1; <WhenAll>d__.<>t__builder.Start<<WhenAll>d__9<T>>(ref <WhenAll>d__); return <WhenAll>d__.<>t__builder.get_Task(); } public static ValueTask WhenAll(ValueTask task1, ValueTask task2) { return Synchronization.WhenAll<(ValueTask, ValueTask)>((task1, task2)); } [System.Runtime.CompilerServices.NullableContext(2)] [AsyncStateMachine(typeof(<WhenAll>d__11<, >))] [AsyncMethodBuilder(typeof(PoolingAsyncValueTaskMethodBuilder))] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 0, 0, 1, 0, 1 })] public static ValueTask<(Result<T1>, Result<T2>)> WhenAll<T1, T2>([System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] ValueTask<T1> task1, [System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] ValueTask<T2> task2) { <WhenAll>d__11<T1, T2> <WhenAll>d__ = default(<WhenAll>d__11<T1, T2>); <WhenAll>d__.<>t__builder = PoolingAsyncValueTaskMethodBuilder<(Result<?>, Result<?>)>.Create(); <WhenAll>d__.task1 = task1; <WhenAll>d__.task2 = task2; <WhenAll>d__.<>1__state = -1; <WhenAll>d__.<>t__builder.Start<<WhenAll>d__11<T1, T2>>(ref <WhenAll>d__); return <WhenAll>d__.<>t__builder.get_Task(); } public static ValueTask WhenAll(ValueTask task1, ValueTask task2, ValueTask task3) { return Synchronization.WhenAll<(ValueTask, ValueTask, ValueTask)>((task1, task2, task3)); } [System.Runtime.CompilerServices.NullableContext(2)] [AsyncStateMachine(typeof(<WhenAll>d__13<, , >))] [AsyncMethodBuilder(typeof(PoolingAsyncValueTaskMethodBuilder))] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 0, 0, 1, 0, 1, 0, 1 })] public static ValueTask<(Result<T1>, Result<T2>, Result<T3>)> WhenAll<T1, T2, T3>([System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] ValueTask<T1> task1, [System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] ValueTask<T2> task2, [System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] ValueTask<T3> task3) { <WhenAll>d__13<T1, T2, T3> <WhenAll>d__ = default(<WhenAll>d__13<T1, T2, T3>); <WhenAll>d__.<>t__builder = PoolingAsyncValueTaskMethodBuilder<(Result<?>, Result<?>, Result<?>)>.Create(); <WhenAll>d__.task1 = task1; <WhenAll>d__.task2 = task2; <WhenAll>d__.task3 = task3; <WhenAll>d__.<>1__state = -1; <WhenAll>d__.<>t__builder.Start<<WhenAll>d__13<T1, T2, T3>>(ref <WhenAll>d__); return <WhenAll>d__.<>t__builder.get_Task(); } public static ValueTask WhenAll(ValueTask task1, ValueTask task2, ValueTask task3, ValueTask task4) { return Synchronization.WhenAll<(ValueTask, ValueTask, ValueTask, ValueTask)>((task1, task2, task3, task4)); } [System.Runtime.CompilerServices.NullableContext(2)] [AsyncStateMachine(typeof(<WhenAll>d__15<, , , >))] [AsyncMethodBuilder(typeof(PoolingAsyncValueTaskMethodBuilder))] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 0, 0, 1, 0, 1, 0, 1, 0, 1 })] public static ValueTask<(Result<T1>, Result<T2>, Result<T3>, Result<T4>)> WhenAll<T1, T2, T3, T4>([System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] ValueTask<T1> task1, [System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] ValueTask<T2> task2, [System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] ValueTask<T3> task3, [System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] ValueTask<T4> task4) { <WhenAll>d__15<T1, T2, T3, T4> <WhenAll>d__ = default(<WhenAll>d__15<T1, T2, T3, T4>); <WhenAll>d__.<>t__builder = PoolingAsyncValueTaskMethodBuilder<(Result<?>, Result<?>, Result<?>, Result<?>)>.Create(); <WhenAll>d__.task1 = task1; <WhenAll>d__.task2 = task2; <WhenAll>d__.task3 = task3; <WhenAll>d__.task4 = task4; <WhenAll>d__.<>1__state = -1; <WhenAll>d__.<>t__builder.Start<<WhenAll>d__15<T1, T2, T3, T4>>(ref <WhenAll>d__); return <WhenAll>d__.<>t__builder.get_Task(); } public static ValueTask WhenAll(ValueTask task1, ValueTask task2, ValueTask task3, ValueTask task4, ValueTask task5) { return Synchronization.WhenAll<(ValueTask, ValueTask, ValueTask, ValueTask, ValueTask)>((task1, task2, task3, task4, task5)); } [System.Runtime.CompilerServices.NullableContext(2)] [AsyncStateMachine(typeof(<WhenAll>d__17<, , , , >))] [AsyncMethodBuilder(typeof(PoolingAsyncValueTaskMethodBuilder))] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1 })] public static ValueTask<(Result<T1>, Result<T2>, Result<T3>, Result<T4>, Result<T5>)> WhenAll<T1, T2, T3, T4, T5>([System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] ValueTask<T1> task1, [System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] ValueTask<T2> task2, [System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] ValueTask<T3> task3, [System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] ValueTask<T4> task4, [System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] ValueTask<T5> task5) { <WhenAll>d__17<T1, T2, T3, T4, T5> <WhenAll>d__ = default(<WhenAll>d__17<T1, T2, T3, T4, T5>); <WhenAll>d__.<>t__builder = PoolingAsyncValueTaskMethodBuilder<(Result<?>, Result<?>, Result<?>, Result<?>, Result<?>)>.Create(); <WhenAll>d__.task1 = task1; <WhenAll>d__.task2 = task2; <WhenAll>d__.task3 = task3; <WhenAll>d__.task4 = task4; <WhenAll>d__.task5 = task5; <WhenAll>d__.<>1__state = -1; <WhenAll>d__.<>t__builder.Start<<WhenAll>d__17<T1, T2, T3, T4, T5>>(ref <WhenAll>d__); return <WhenAll>d__.<>t__builder.get_Task(); } } }