AsyncDelegate
Provides set of methods for asynchronous invocation of various delegates.
using DotNext.Runtime.CompilerServices;
using System;
using System.Threading;
namespace DotNext.Threading
{
public static class AsyncDelegate
{
public static AsyncDelegateFuture InvokeAsync<D>(this D delegate, Action<D> invoker, CancellationToken token = default(CancellationToken)) where D : MulticastDelegate
{
if (!token.IsCancellationRequested)
return ((AsyncDelegateFuture<D>)new CustomDelegateFuture<D>(invoker, token)).Invoke(delegate);
return CanceledAsyncDelegateFuture.Instance;
}
public static AsyncDelegateFuture InvokeAsync<E>(this EventHandler<E> handler, object sender, E args, CancellationToken token = default(CancellationToken))
{
if (!token.IsCancellationRequested)
return ((AsyncDelegateFuture<EventHandler<E>>)new EventHandlerFuture<E>(sender, args, token)).Invoke(handler);
return CanceledAsyncDelegateFuture.Instance;
}
public static AsyncDelegateFuture InvokeAsync(this EventHandler handler, object sender, EventArgs args, CancellationToken token = default(CancellationToken))
{
if (!token.IsCancellationRequested)
return new EventHandlerFuture(sender, args, token).Invoke(handler);
return CanceledAsyncDelegateFuture.Instance;
}
public static AsyncDelegateFuture InvokeAsync(this Action action, CancellationToken token = default(CancellationToken))
{
if (!token.IsCancellationRequested)
return new ActionFuture(token).Invoke(action);
return CanceledAsyncDelegateFuture.Instance;
}
public static AsyncDelegateFuture InvokeAsync<T>(this Action<T> action, T arg, CancellationToken token = default(CancellationToken))
{
if (!token.IsCancellationRequested)
return ((AsyncDelegateFuture<Action<T>>)new ActionFuture<T>(arg, token)).Invoke(action);
return CanceledAsyncDelegateFuture.Instance;
}
public static AsyncDelegateFuture InvokeAsync<T1, T2>(this Action<T1, T2> action, T1 arg1, T2 arg2, CancellationToken token = default(CancellationToken))
{
if (!token.IsCancellationRequested)
return ((AsyncDelegateFuture<Action<T1, T2>>)new ActionFuture<T1, T2>(arg1, arg2, token)).Invoke(action);
return CanceledAsyncDelegateFuture.Instance;
}
public static AsyncDelegateFuture InvokeAsync<T1, T2, T3>(this Action<T1, T2, T3> action, T1 arg1, T2 arg2, T3 arg3, CancellationToken token = default(CancellationToken))
{
if (!token.IsCancellationRequested)
return ((AsyncDelegateFuture<Action<T1, T2, T3>>)new ActionFuture<T1, T2, T3>(arg1, arg2, arg3, token)).Invoke(action);
return CanceledAsyncDelegateFuture.Instance;
}
public static AsyncDelegateFuture InvokeAsync<T1, T2, T3, T4>(this Action<T1, T2, T3, T4> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, CancellationToken token = default(CancellationToken))
{
if (!token.IsCancellationRequested)
return ((AsyncDelegateFuture<Action<T1, T2, T3, T4>>)new ActionFuture<T1, T2, T3, T4>(arg1, arg2, arg3, arg4, token)).Invoke(action);
return CanceledAsyncDelegateFuture.Instance;
}
public static AsyncDelegateFuture InvokeAsync<T1, T2, T3, T4, T5>(this Action<T1, T2, T3, T4, T5> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, CancellationToken token = default(CancellationToken))
{
if (!token.IsCancellationRequested)
return ((AsyncDelegateFuture<Action<T1, T2, T3, T4, T5>>)new ActionFuture<T1, T2, T3, T4, T5>(arg1, arg2, arg3, arg4, arg5, token)).Invoke(action);
return CanceledAsyncDelegateFuture.Instance;
}
public static AsyncDelegateFuture InvokeAsync<T1, T2, T3, T4, T5, 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))
{
if (!token.IsCancellationRequested)
return ((AsyncDelegateFuture<Action<T1, T2, T3, T4, T5, T6>>)new ActionFuture<T1, T2, T3, T4, T5, T6>(arg1, arg2, arg3, arg4, arg5, arg6, token)).Invoke(action);
return CanceledAsyncDelegateFuture.Instance;
}
public static AsyncDelegateFuture InvokeAsync<T1, T2, T3, T4, T5, T6, 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))
{
if (!token.IsCancellationRequested)
return ((AsyncDelegateFuture<Action<T1, T2, T3, T4, T5, T6, T7>>)new ActionFuture<T1, T2, T3, T4, T5, T6, T7>(arg1, arg2, arg3, arg4, arg5, arg6, arg7, token)).Invoke(action);
return CanceledAsyncDelegateFuture.Instance;
}
public static AsyncDelegateFuture InvokeAsync<T1, T2, T3, T4, T5, T6, T7, 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))
{
if (!token.IsCancellationRequested)
return ((AsyncDelegateFuture<Action<T1, T2, T3, T4, T5, T6, T7, T8>>)new ActionFuture<T1, T2, T3, T4, T5, T6, T7, T8>(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, token)).Invoke(action);
return CanceledAsyncDelegateFuture.Instance;
}
public static AsyncDelegateFuture InvokeAsync<T1, T2, T3, T4, T5, T6, T7, T8, 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))
{
if (!token.IsCancellationRequested)
return ((AsyncDelegateFuture<Action<T1, T2, T3, T4, T5, T6, T7, T8, T9>>)new ActionFuture<T1, T2, T3, T4, T5, T6, T7, T8, T9>(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, token)).Invoke(action);
return CanceledAsyncDelegateFuture.Instance;
}
public static AsyncDelegateFuture InvokeAsync<T1, T2, T3, T4, T5, T6, T7, T8, T9, 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))
{
if (!token.IsCancellationRequested)
return ((AsyncDelegateFuture<Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>>)new ActionFuture<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, token)).Invoke(action);
return CanceledAsyncDelegateFuture.Instance;
}
}
}