DotNext by .NET Foundation and Contributors

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

 AsyncDelegate

public static class AsyncDelegate
Provides set of methods for asynchronous invocation of various delegates.
using DotNext.Runtime.ExceptionServices; using DotNext.Threading.Tasks; using System; using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; namespace DotNext.Threading { [System.Runtime.CompilerServices.NullableContext(1)] [System.Runtime.CompilerServices.Nullable(0)] public static class AsyncDelegate { private sealed class InvocationWorkItem<TDelegate, TContext> where TDelegate : MulticastDelegate { private readonly CancellationToken token; private readonly TDelegate invocationList; private readonly TContext context; private readonly IntPtr invoker; internal InvocationWorkItem(TDelegate invocationList, IntPtr invoker, [In] [IsReadOnly] ref TContext context, CancellationToken token) { this.token = token; this.invocationList = invocationList; this.invoker = invoker; this.context = context; } internal void Invoke() { ExceptionAggregator exceptionAggregator = default(ExceptionAggregator); Delegate[] array = invocationList.GetInvocationList(); for (int i = 0; i < array.Length; i++) { TDelegate val = (TDelegate)array[i]; if (token.IsCancellationRequested) { exceptionAggregator.Add(new OperationCanceledException(token)); break; } try { IntPtr intPtr = invoker; ; } catch (Exception e) { exceptionAggregator.Add(e); } } exceptionAggregator.ThrowIfNeeded(); } } private static Task InvokeAsync<TDelegate, TContext>(TDelegate delegate, [In] [IsReadOnly] ref TContext context, IntPtr invoker, CancellationToken token) where TDelegate : MulticastDelegate { if (!token.IsCancellationRequested) return Task.Factory.StartNew(new InvocationWorkItem<TDelegate, TContext>(delegate, invoker, ref context, token).Invoke, token, TaskCreationOptions.None, TaskScheduler.Default); return Task.FromCanceled(token); } public unsafe static Task InvokeAsync<[System.Runtime.CompilerServices.Nullable(0)] TDelegate>(this TDelegate delegate, Action<TDelegate> invoker, CancellationToken token = default(CancellationToken)) where TDelegate : MulticastDelegate { return InvokeAsync(delegate, ref invoker, (IntPtr)(void*), token); } public unsafe static Task InvokeAsync<[System.Runtime.CompilerServices.Nullable(2)] TEventArgs>(this EventHandler<TEventArgs> handler, object sender, TEventArgs args, CancellationToken token = default(CancellationToken)) { (object, TEventArgs) context = (sender, args); return InvokeAsync(handler, ref context, (IntPtr)(void*), token); } public unsafe static Task InvokeAsync(this EventHandler handler, object sender, EventArgs args, CancellationToken token = default(CancellationToken)) { (object, EventArgs) context = (sender, args); return InvokeAsync(handler, ref context, (IntPtr)(void*), token); } public unsafe static Task InvokeAsync(this Action action, CancellationToken token = default(CancellationToken)) { return InvokeAsync(action, ref Missing.Value, (IntPtr)(void*), token); } public unsafe static Task InvokeAsync<[System.Runtime.CompilerServices.Nullable(2)] T>(this Action<T> action, T arg, CancellationToken token = default(CancellationToken)) { return InvokeAsync(action, ref arg, (IntPtr)(void*), token); } public unsafe static Task InvokeAsync<[System.Runtime.CompilerServices.Nullable(2)] T1, [System.Runtime.CompilerServices.Nullable(2)] T2>(this Action<T1, T2> action, T1 arg1, T2 arg2, CancellationToken token = default(CancellationToken)) { (T1, T2) context = (arg1, arg2); return InvokeAsync(action, ref context, (IntPtr)(void*), token); } public unsafe static Task InvokeAsync<[System.Runtime.CompilerServices.Nullable(2)] T1, [System.Runtime.CompilerServices.Nullable(2)] T2, [System.Runtime.CompilerServices.Nullable(2)] T3>(this Action<T1, T2, T3> action, T1 arg1, T2 arg2, T3 arg3, CancellationToken token = default(CancellationToken)) { (T1, T2, T3) context = (arg1, arg2, arg3); return InvokeAsync(action, ref context, (IntPtr)(void*), token); } public unsafe static Task InvokeAsync<[System.Runtime.CompilerServices.Nullable(2)] T1, [System.Runtime.CompilerServices.Nullable(2)] T2, [System.Runtime.CompilerServices.Nullable(2)] T3, [System.Runtime.CompilerServices.Nullable(2)] T4>(this Action<T1, T2, T3, T4> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, CancellationToken token = default(CancellationToken)) { (T1, T2, T3, T4) context = (arg1, arg2, arg3, arg4); return InvokeAsync(action, ref context, (IntPtr)(void*), token); } public unsafe static Task InvokeAsync<[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>(this Action<T1, T2, T3, T4, T5> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, CancellationToken token = default(CancellationToken)) { (T1, T2, T3, T4, T5) context = (arg1, arg2, arg3, arg4, arg5); return InvokeAsync(action, ref context, (IntPtr)(void*), token); } public unsafe static Task InvokeAsync<[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, [System.Runtime.CompilerServices.Nullable(2)] T6>(this Action<T1, T2, T3, T4, T5, T6> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, CancellationToken token = default(CancellationToken)) { (T1, T2, T3, T4, T5, T6) context = (arg1, arg2, arg3, arg4, arg5, arg6); return InvokeAsync(action, ref context, (IntPtr)(void*), token); } public unsafe static Task InvokeAsync<[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, [System.Runtime.CompilerServices.Nullable(2)] T6, [System.Runtime.CompilerServices.Nullable(2)] T7>(this Action<T1, T2, T3, T4, T5, T6, T7> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, CancellationToken token = default(CancellationToken)) { (T1, T2, T3, T4, T5, T6, T7) context = (arg1, arg2, arg3, arg4, arg5, arg6, arg7); return InvokeAsync(action, ref context, (IntPtr)(void*), token); } public unsafe static Task InvokeAsync<[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, [System.Runtime.CompilerServices.Nullable(2)] T6, [System.Runtime.CompilerServices.Nullable(2)] T7, [System.Runtime.CompilerServices.Nullable(2)] T8>(this Action<T1, T2, T3, T4, T5, T6, T7, T8> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, CancellationToken token = default(CancellationToken)) { (T1, T2, T3, T4, T5, T6, T7, T8) context = (arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8); return InvokeAsync(action, ref context, (IntPtr)(void*), token); } public unsafe static Task InvokeAsync<[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, [System.Runtime.CompilerServices.Nullable(2)] T6, [System.Runtime.CompilerServices.Nullable(2)] T7, [System.Runtime.CompilerServices.Nullable(2)] T8, [System.Runtime.CompilerServices.Nullable(2)] T9>(this Action<T1, T2, T3, T4, T5, T6, T7, T8, T9> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, CancellationToken token = default(CancellationToken)) { (T1, T2, T3, T4, T5, T6, T7, T8, T9) context = (arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9); return InvokeAsync(action, ref context, (IntPtr)(void*), token); } public unsafe static Task InvokeAsync<[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, [System.Runtime.CompilerServices.Nullable(2)] T6, [System.Runtime.CompilerServices.Nullable(2)] T7, [System.Runtime.CompilerServices.Nullable(2)] T8, [System.Runtime.CompilerServices.Nullable(2)] T9, [System.Runtime.CompilerServices.Nullable(2)] T10>(this Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, CancellationToken token = default(CancellationToken)) { (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10) context = (arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10); return InvokeAsync(action, ref context, (IntPtr)(void*), token); } [System.Runtime.CompilerServices.NullableContext(2)] [return: System.Runtime.CompilerServices.Nullable(1)] public static Task BeginInvoke([System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] this Action<object> action, object state, AsyncCallback callback, TaskCreationOptions options = TaskCreationOptions.None, TaskScheduler scheduler = null) { Task task = Task.Factory.StartNew(action, state, CancellationToken.None, options, scheduler ?? TaskScheduler.Default); if (callback != null) task.OnCompleted(callback); return task; } [System.Runtime.CompilerServices.NullableContext(2)] [return: System.Runtime.CompilerServices.Nullable(1)] public static Task BeginInvoke([System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] this Action<object, CancellationToken> action, object state, AsyncCallback callback, TaskCreationOptions options = TaskCreationOptions.None, TaskScheduler scheduler = null, CancellationToken token = default(CancellationToken)) { Task task = Task.Factory.StartNew(delegate(object s) { action(s, token); }, state, token, options, scheduler ?? TaskScheduler.Default); if (callback != null) task.OnCompleted(callback); return task; } [System.Runtime.CompilerServices.NullableContext(2)] [return: System.Runtime.CompilerServices.Nullable(1)] public static Task<TResult> BeginInvoke<TResult>([System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2, 1 })] this Func<object, TResult> function, object state, AsyncCallback callback, TaskCreationOptions options = TaskCreationOptions.None, TaskScheduler scheduler = null) { Task<TResult> task = Task<TResult>.Factory.StartNew(function, state, CancellationToken.None, options, scheduler ?? TaskScheduler.Default); if (callback != null) task.OnCompleted(callback); return task; } [System.Runtime.CompilerServices.NullableContext(2)] [return: System.Runtime.CompilerServices.Nullable(1)] public static Task<TResult> BeginInvoke<TResult>([System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2, 1 })] this Func<object, CancellationToken, TResult> function, object state, AsyncCallback callback, TaskCreationOptions options = TaskCreationOptions.None, TaskScheduler scheduler = null, CancellationToken token = default(CancellationToken)) { Task<TResult> task = Task<TResult>.Factory.StartNew((Func<object, TResult>)((object s) => function(s, token)), state, token, options, scheduler ?? TaskScheduler.Default); if (callback != null) task.OnCompleted(callback); return task; } } }