Last active
June 6, 2023 09:20
-
-
Save rkttu/8259b642c26d5b3848ab to your computer and use it in GitHub Desktop.
Async/Await Alternative Demonstration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace System.Threading.Tasks | |
{ | |
public static class DelegateInvokeAsyncExtension | |
{ | |
public static Task<TResult> InvokeAsync<TResult>( | |
this Func<TResult> function, | |
AsyncCallback callback = null, | |
object state = null, | |
TaskCreationOptions creationOptions = default(TaskCreationOptions), | |
TaskScheduler scheduler = default(TaskScheduler)) | |
{ | |
return Task<TResult>.Factory.FromAsync( | |
function.BeginInvoke(callback, state), | |
function.EndInvoke, | |
creationOptions, | |
scheduler ?? TaskScheduler.Current ?? TaskScheduler.Default); | |
} | |
public static Task<TResult> InvokeAsync<TResult, T>( | |
this Func<T, TResult> function, | |
T arg, | |
AsyncCallback callback = null, | |
object state = null, | |
TaskCreationOptions creationOptions = default(TaskCreationOptions), | |
TaskScheduler scheduler = default(TaskScheduler)) | |
{ | |
return Task<TResult>.Factory.FromAsync( | |
function.BeginInvoke(arg, callback, state), | |
function.EndInvoke, | |
creationOptions, | |
scheduler ?? TaskScheduler.Current ?? TaskScheduler.Default); | |
} | |
public static Task<TResult> InvokeAsync<TResult, T1, T2>( | |
this Func<T1, T2, TResult> function, | |
T1 arg1, T2 arg2, | |
AsyncCallback callback = null, | |
object state = null, | |
TaskCreationOptions creationOptions = default(TaskCreationOptions), | |
TaskScheduler scheduler = default(TaskScheduler)) | |
{ | |
return Task<TResult>.Factory.FromAsync( | |
function.BeginInvoke(arg1, arg2, callback, state), | |
function.EndInvoke, | |
creationOptions, | |
scheduler ?? TaskScheduler.Current ?? TaskScheduler.Default); | |
} | |
public static Task<TResult> InvokeAsync<TResult, T1, T2, T3>( | |
this Func<T1, T2, T3, TResult> function, | |
T1 arg1, T2 arg2, T3 arg3, | |
AsyncCallback callback = null, | |
object state = null, | |
TaskCreationOptions creationOptions = default(TaskCreationOptions), | |
TaskScheduler scheduler = default(TaskScheduler)) | |
{ | |
return Task<TResult>.Factory.FromAsync( | |
function.BeginInvoke(arg1, arg2, arg3, callback, state), | |
function.EndInvoke, | |
creationOptions, | |
scheduler ?? TaskScheduler.Current ?? TaskScheduler.Default); | |
} | |
public static Task<TResult> InvokeAsync<TResult, T1, T2, T3, T4>( | |
this Func<T1, T2, T3, T4, TResult> function, | |
T1 arg1, T2 arg2, T3 arg3, T4 arg4, | |
AsyncCallback callback = null, | |
object state = null, | |
TaskCreationOptions creationOptions = default(TaskCreationOptions), | |
TaskScheduler scheduler = default(TaskScheduler)) | |
{ | |
return Task<TResult>.Factory.FromAsync( | |
function.BeginInvoke(arg1, arg2, arg3, arg4, callback, state), | |
function.EndInvoke, | |
creationOptions, | |
scheduler ?? TaskScheduler.Current ?? TaskScheduler.Default); | |
} | |
public static Task<TResult> InvokeAsync<TResult, T1, T2, T3, T4, T5>( | |
this Func<T1, T2, T3, T4, T5, TResult> function, | |
T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, | |
AsyncCallback callback = null, | |
object state = null, | |
TaskCreationOptions creationOptions = default(TaskCreationOptions), | |
TaskScheduler scheduler = default(TaskScheduler)) | |
{ | |
return Task<TResult>.Factory.FromAsync( | |
function.BeginInvoke(arg1, arg2, arg3, arg4, arg5, callback, state), | |
function.EndInvoke, | |
creationOptions, | |
scheduler ?? TaskScheduler.Current ?? TaskScheduler.Default); | |
} | |
public static Task<TResult> InvokeAsync<TResult, T1, T2, T3, T4, T5, T6>( | |
this Func<T1, T2, T3, T4, T5, T6, TResult> function, | |
T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, | |
AsyncCallback callback = null, | |
object state = null, | |
TaskCreationOptions creationOptions = default(TaskCreationOptions), | |
TaskScheduler scheduler = default(TaskScheduler)) | |
{ | |
return Task<TResult>.Factory.FromAsync( | |
function.BeginInvoke(arg1, arg2, arg3, arg4, arg5, arg6, callback, state), | |
function.EndInvoke, | |
creationOptions, | |
scheduler ?? TaskScheduler.Current ?? TaskScheduler.Default); | |
} | |
public static Task<TResult> InvokeAsync<TResult, T1, T2, T3, T4, T5, T6, T7>( | |
this Func<T1, T2, T3, T4, T5, T6, T7, TResult> function, | |
T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, | |
AsyncCallback callback = null, | |
object state = null, | |
TaskCreationOptions creationOptions = default(TaskCreationOptions), | |
TaskScheduler scheduler = default(TaskScheduler)) | |
{ | |
return Task<TResult>.Factory.FromAsync( | |
function.BeginInvoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7, callback, state), | |
function.EndInvoke, | |
creationOptions, | |
scheduler ?? TaskScheduler.Current ?? TaskScheduler.Default); | |
} | |
public static Task<TResult> InvokeAsync<TResult, T1, T2, T3, T4, T5, T6, T7, T8>( | |
this Func<T1, T2, T3, T4, T5, T6, T7, T8, TResult> function, | |
T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, | |
AsyncCallback callback = null, | |
object state = null, | |
TaskCreationOptions creationOptions = default(TaskCreationOptions), | |
TaskScheduler scheduler = default(TaskScheduler)) | |
{ | |
return Task<TResult>.Factory.FromAsync( | |
function.BeginInvoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, callback, state), | |
function.EndInvoke, | |
creationOptions, | |
scheduler ?? TaskScheduler.Current ?? TaskScheduler.Default); | |
} | |
public static Task<TResult> InvokeAsync<TResult, T1, T2, T3, T4, T5, T6, T7, T8, T9>( | |
this Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, TResult> function, | |
T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, | |
AsyncCallback callback = null, | |
object state = null, | |
TaskCreationOptions creationOptions = default(TaskCreationOptions), | |
TaskScheduler scheduler = default(TaskScheduler)) | |
{ | |
return Task<TResult>.Factory.FromAsync( | |
function.BeginInvoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, callback, state), | |
function.EndInvoke, | |
creationOptions, | |
scheduler ?? TaskScheduler.Current ?? TaskScheduler.Default); | |
} | |
public static Task<TResult> InvokeAsync<TResult, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>( | |
this Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, TResult> function, | |
T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, | |
AsyncCallback callback = null, | |
object state = null, | |
TaskCreationOptions creationOptions = default(TaskCreationOptions), | |
TaskScheduler scheduler = default(TaskScheduler)) | |
{ | |
return Task<TResult>.Factory.FromAsync( | |
function.BeginInvoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, callback, state), | |
function.EndInvoke, | |
creationOptions, | |
scheduler ?? TaskScheduler.Current ?? TaskScheduler.Default); | |
} | |
public static Task<TResult> InvokeAsync<TResult, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11>( | |
this Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, TResult> function, | |
T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, | |
AsyncCallback callback = null, | |
object state = null, | |
TaskCreationOptions creationOptions = default(TaskCreationOptions), | |
TaskScheduler scheduler = default(TaskScheduler)) | |
{ | |
return Task<TResult>.Factory.FromAsync( | |
function.BeginInvoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, callback, state), | |
function.EndInvoke, | |
creationOptions, | |
scheduler ?? TaskScheduler.Current ?? TaskScheduler.Default); | |
} | |
public static Task<TResult> InvokeAsync<TResult, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12>( | |
this Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, TResult> function, | |
T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, | |
AsyncCallback callback = null, | |
object state = null, | |
TaskCreationOptions creationOptions = default(TaskCreationOptions), | |
TaskScheduler scheduler = default(TaskScheduler)) | |
{ | |
return Task<TResult>.Factory.FromAsync( | |
function.BeginInvoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, callback, state), | |
function.EndInvoke, | |
creationOptions, | |
scheduler ?? TaskScheduler.Current ?? TaskScheduler.Default); | |
} | |
public static Task<TResult> InvokeAsync<TResult, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13>( | |
this Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, TResult> function, | |
T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, | |
AsyncCallback callback = null, | |
object state = null, | |
TaskCreationOptions creationOptions = default(TaskCreationOptions), | |
TaskScheduler scheduler = default(TaskScheduler)) | |
{ | |
return Task<TResult>.Factory.FromAsync( | |
function.BeginInvoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, callback, state), | |
function.EndInvoke, | |
creationOptions, | |
scheduler ?? TaskScheduler.Current ?? TaskScheduler.Default); | |
} | |
public static Task<TResult> InvokeAsync<TResult, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14>( | |
this Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, TResult> function, | |
T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, | |
AsyncCallback callback = null, | |
object state = null, | |
TaskCreationOptions creationOptions = default(TaskCreationOptions), | |
TaskScheduler scheduler = default(TaskScheduler)) | |
{ | |
return Task<TResult>.Factory.FromAsync( | |
function.BeginInvoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, callback, state), | |
function.EndInvoke, | |
creationOptions, | |
scheduler ?? TaskScheduler.Current ?? TaskScheduler.Default); | |
} | |
public static Task<TResult> InvokeAsync<TResult, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15>( | |
this Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, TResult> function, | |
T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15, | |
AsyncCallback callback = null, | |
object state = null, | |
TaskCreationOptions creationOptions = default(TaskCreationOptions), | |
TaskScheduler scheduler = default(TaskScheduler)) | |
{ | |
return Task<TResult>.Factory.FromAsync( | |
function.BeginInvoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15, callback, state), | |
function.EndInvoke, | |
creationOptions, | |
scheduler ?? TaskScheduler.Current ?? TaskScheduler.Default); | |
} | |
public static Task<TResult> InvokeAsync<TResult, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16>( | |
this Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, TResult> function, | |
T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15, T16 arg16, | |
AsyncCallback callback = null, | |
object state = null, | |
TaskCreationOptions creationOptions = default(TaskCreationOptions), | |
TaskScheduler scheduler = default(TaskScheduler)) | |
{ | |
return Task<TResult>.Factory.FromAsync( | |
function.BeginInvoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15, arg16, callback, state), | |
function.EndInvoke, | |
creationOptions, | |
scheduler ?? TaskScheduler.Current ?? TaskScheduler.Default); | |
} | |
public static Task InvokeAsync( | |
this Action action, | |
AsyncCallback callback = null, | |
object state = null, | |
TaskCreationOptions creationOptions = default(TaskCreationOptions), | |
TaskScheduler scheduler = default(TaskScheduler)) | |
{ | |
return Task.Factory.FromAsync( | |
action.BeginInvoke(callback, state), | |
action.EndInvoke, | |
creationOptions, | |
scheduler ?? TaskScheduler.Current ?? TaskScheduler.Default); | |
} | |
public static Task InvokeAsync<T>( | |
this Action<T> action, | |
T obj, | |
AsyncCallback callback = null, | |
object state = null, | |
TaskCreationOptions creationOptions = default(TaskCreationOptions), | |
TaskScheduler scheduler = default(TaskScheduler)) | |
{ | |
return Task.Factory.FromAsync( | |
action.BeginInvoke(obj, callback, state), | |
action.EndInvoke, | |
creationOptions, | |
scheduler ?? TaskScheduler.Current ?? TaskScheduler.Default); | |
} | |
public static Task InvokeAsync<T1, T2>( | |
this Action<T1, T2> action, | |
T1 arg1, T2 arg2, | |
AsyncCallback callback = null, | |
object state = null, | |
TaskCreationOptions creationOptions = default(TaskCreationOptions), | |
TaskScheduler scheduler = default(TaskScheduler)) | |
{ | |
return Task.Factory.FromAsync( | |
action.BeginInvoke(arg1, arg2, callback, state), | |
action.EndInvoke, | |
creationOptions, | |
scheduler ?? TaskScheduler.Current ?? TaskScheduler.Default); | |
} | |
public static Task InvokeAsync<T1, T2, T3>( | |
this Action<T1, T2, T3> action, | |
T1 arg1, T2 arg2, T3 arg3, | |
AsyncCallback callback = null, | |
object state = null, | |
TaskCreationOptions creationOptions = default(TaskCreationOptions), | |
TaskScheduler scheduler = default(TaskScheduler)) | |
{ | |
return Task.Factory.FromAsync( | |
action.BeginInvoke(arg1, arg2, arg3, callback, state), | |
action.EndInvoke, | |
creationOptions, | |
scheduler ?? TaskScheduler.Current ?? TaskScheduler.Default); | |
} | |
public static Task InvokeAsync<T1, T2, T3, T4>( | |
this Action<T1, T2, T3, T4> action, | |
T1 arg1, T2 arg2, T3 arg3, T4 arg4, | |
AsyncCallback callback = null, | |
object state = null, | |
TaskCreationOptions creationOptions = default(TaskCreationOptions), | |
TaskScheduler scheduler = default(TaskScheduler)) | |
{ | |
return Task.Factory.FromAsync( | |
action.BeginInvoke(arg1, arg2, arg3, arg4, callback, state), | |
action.EndInvoke, | |
creationOptions, | |
scheduler ?? TaskScheduler.Current ?? TaskScheduler.Default); | |
} | |
public static Task InvokeAsync<T1, T2, T3, T4, T5>( | |
this Action<T1, T2, T3, T4, T5> action, | |
T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, | |
AsyncCallback callback = null, | |
object state = null, | |
TaskCreationOptions creationOptions = default(TaskCreationOptions), | |
TaskScheduler scheduler = default(TaskScheduler)) | |
{ | |
return Task.Factory.FromAsync( | |
action.BeginInvoke(arg1, arg2, arg3, arg4, arg5, callback, state), | |
action.EndInvoke, | |
creationOptions, | |
scheduler ?? TaskScheduler.Current ?? TaskScheduler.Default); | |
} | |
public static Task 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, | |
AsyncCallback callback = null, | |
object state = null, | |
TaskCreationOptions creationOptions = default(TaskCreationOptions), | |
TaskScheduler scheduler = default(TaskScheduler)) | |
{ | |
return Task.Factory.FromAsync( | |
action.BeginInvoke(arg1, arg2, arg3, arg4, arg5, arg6, callback, state), | |
action.EndInvoke, | |
creationOptions, | |
scheduler ?? TaskScheduler.Current ?? TaskScheduler.Default); | |
} | |
public static Task 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, | |
AsyncCallback callback = null, | |
object state = null, | |
TaskCreationOptions creationOptions = default(TaskCreationOptions), | |
TaskScheduler scheduler = default(TaskScheduler)) | |
{ | |
return Task.Factory.FromAsync( | |
action.BeginInvoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7, callback, state), | |
action.EndInvoke, | |
creationOptions, | |
scheduler ?? TaskScheduler.Current ?? TaskScheduler.Default); | |
} | |
public static Task 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, | |
AsyncCallback callback = null, | |
object state = null, | |
TaskCreationOptions creationOptions = default(TaskCreationOptions), | |
TaskScheduler scheduler = default(TaskScheduler)) | |
{ | |
return Task.Factory.FromAsync( | |
action.BeginInvoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, callback, state), | |
action.EndInvoke, | |
creationOptions, | |
scheduler ?? TaskScheduler.Current ?? TaskScheduler.Default); | |
} | |
public static Task 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, | |
AsyncCallback callback = null, | |
object state = null, | |
TaskCreationOptions creationOptions = default(TaskCreationOptions), | |
TaskScheduler scheduler = default(TaskScheduler)) | |
{ | |
return Task.Factory.FromAsync( | |
action.BeginInvoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, callback, state), | |
action.EndInvoke, | |
creationOptions, | |
scheduler ?? TaskScheduler.Current ?? TaskScheduler.Default); | |
} | |
public static Task 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, | |
AsyncCallback callback = null, | |
object state = null, | |
TaskCreationOptions creationOptions = default(TaskCreationOptions), | |
TaskScheduler scheduler = default(TaskScheduler)) | |
{ | |
return Task.Factory.FromAsync( | |
action.BeginInvoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, callback, state), | |
action.EndInvoke, | |
creationOptions, | |
scheduler ?? TaskScheduler.Current ?? TaskScheduler.Default); | |
} | |
public static Task InvokeAsync<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11>( | |
this Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> action, | |
T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, | |
AsyncCallback callback = null, | |
object state = null, | |
TaskCreationOptions creationOptions = default(TaskCreationOptions), | |
TaskScheduler scheduler = default(TaskScheduler)) | |
{ | |
return Task.Factory.FromAsync( | |
action.BeginInvoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, callback, state), | |
action.EndInvoke, | |
creationOptions, | |
scheduler ?? TaskScheduler.Current ?? TaskScheduler.Default); | |
} | |
public static Task InvokeAsync<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12>( | |
this Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12> action, | |
T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, | |
AsyncCallback callback = null, | |
object state = null, | |
TaskCreationOptions creationOptions = default(TaskCreationOptions), | |
TaskScheduler scheduler = default(TaskScheduler)) | |
{ | |
return Task.Factory.FromAsync( | |
action.BeginInvoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, callback, state), | |
action.EndInvoke, | |
creationOptions, | |
scheduler ?? TaskScheduler.Current ?? TaskScheduler.Default); | |
} | |
public static Task InvokeAsync<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13>( | |
this Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13> action, | |
T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, | |
AsyncCallback callback = null, | |
object state = null, | |
TaskCreationOptions creationOptions = default(TaskCreationOptions), | |
TaskScheduler scheduler = default(TaskScheduler)) | |
{ | |
return Task.Factory.FromAsync( | |
action.BeginInvoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, callback, state), | |
action.EndInvoke, | |
creationOptions, | |
scheduler ?? TaskScheduler.Current ?? TaskScheduler.Default); | |
} | |
public static Task InvokeAsync<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14>( | |
this Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14> action, | |
T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, | |
AsyncCallback callback = null, | |
object state = null, | |
TaskCreationOptions creationOptions = default(TaskCreationOptions), | |
TaskScheduler scheduler = default(TaskScheduler)) | |
{ | |
return Task.Factory.FromAsync( | |
action.BeginInvoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, callback, state), | |
action.EndInvoke, | |
creationOptions, | |
scheduler ?? TaskScheduler.Current ?? TaskScheduler.Default); | |
} | |
public static Task InvokeAsync<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15>( | |
this Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15> action, | |
T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15, | |
AsyncCallback callback = null, | |
object state = null, | |
TaskCreationOptions creationOptions = default(TaskCreationOptions), | |
TaskScheduler scheduler = default(TaskScheduler)) | |
{ | |
return Task.Factory.FromAsync( | |
action.BeginInvoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15, callback, state), | |
action.EndInvoke, | |
creationOptions, | |
scheduler ?? TaskScheduler.Current ?? TaskScheduler.Default); | |
} | |
public static Task InvokeAsync<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16>( | |
this Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16> action, | |
T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15, T16 arg16, | |
AsyncCallback callback = null, | |
object state = null, | |
TaskCreationOptions creationOptions = default(TaskCreationOptions), | |
TaskScheduler scheduler = default(TaskScheduler)) | |
{ | |
return Task.Factory.FromAsync( | |
action.BeginInvoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15, arg16, callback, state), | |
action.EndInvoke, | |
creationOptions, | |
scheduler ?? TaskScheduler.Current ?? TaskScheduler.Default); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Threading.Tasks; | |
namespace AsyncAwaitAlternative | |
{ | |
static class Program | |
{ | |
static void Main(string[] args) | |
{ | |
int length = SampleCode().ToTask<int>().Result; | |
Console.WriteLine(length); | |
Console.ReadLine(); | |
} | |
static IEnumerable<Task> SampleCode() | |
{ | |
bool result = false; | |
string name = String.Empty; | |
for (int i = 0; i < 3; i++) | |
{ | |
yield return ((Action<string>)Console.Write) | |
.InvokeAsync("What is your name? "); | |
yield return ((Func<string>)Console.ReadLine) | |
.InvokeAsync() | |
.ContinueWith(x => name = x.Result); | |
yield return ((Func<string, bool>)String.IsNullOrWhiteSpace) | |
.InvokeAsync(name) | |
.ContinueWith(x => result = x.Result); | |
if (result) | |
{ | |
yield return ((Action<string>)Console.WriteLine) | |
.InvokeAsync("I want to know your name. Please answer me again."); | |
} | |
else | |
break; | |
} | |
yield return ((Func<string, bool>)String.IsNullOrWhiteSpace) | |
.InvokeAsync(name) | |
.ContinueWith(x => result = x.Result); | |
if (!result) | |
{ | |
yield return ((Action<string, string>)Console.WriteLine) | |
.InvokeAsync("Your name is {0}. Nice to meet you!", name); | |
yield return TaskEx.FromResult(name.Length); | |
} | |
else | |
yield return TaskEx.FromResult(default(int)); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// License: CPOL at http://www.codeproject.com/info/cpol10.aspx | |
// http://www.codeproject.com/Articles/504197/Await-Tasks-in-Csharp-using-Iterators | |
using System.Collections.Generic; | |
namespace System.Threading.Tasks | |
{ | |
/// <summary>Extensions related to the <see cref="Task"/> classes. Supports implementing "async"-style methods in C#4 using iterators.</summary> | |
public static class TaskEx | |
{ | |
/// <summary> | |
/// Return a Completed <see cref="Task{TResult}"/> with a specific <see cref="Task{TResult}.Result"/> value. | |
/// </summary> | |
public static Task<TResult> FromResult<TResult>(TResult resultValue) | |
{ | |
var completionSource = new TaskCompletionSource<TResult>(); | |
completionSource.SetResult(resultValue); | |
return completionSource.Task; | |
} | |
/// <summary>Internal marker type for using <see cref="ToTask{T}"/> to implement <see cref="ToTask"/>.</summary> | |
private abstract class VoidResult { } | |
/// <summary> | |
/// Transform an enumeration of <see cref="Task"/> into a single non-Result <see cref="Task"/>. | |
/// </summary> | |
public static Task ToTask(this IEnumerable<Task> tasks) | |
{ | |
return ToTask<VoidResult>(tasks); | |
} | |
/// <summary> | |
/// Transform an enumeration of <see cref="Task"/> into a single <see cref="Task{TResult}"/>. | |
/// The final <see cref="Task"/> in <paramref name="tasks"/> must be a <see cref="Task{TResult}"/>. | |
/// </summary> | |
public static Task<TResult> ToTask<TResult>(this IEnumerable<Task> tasks) | |
{ | |
var taskScheduler = | |
SynchronizationContext.Current == null | |
? TaskScheduler.Default : TaskScheduler.FromCurrentSynchronizationContext(); | |
var taskEnumerator = tasks.GetEnumerator(); | |
var completionSource = new TaskCompletionSource<TResult>(); | |
ToTaskDoOneStep(taskEnumerator, taskScheduler, completionSource, null); | |
return completionSource.Task; | |
} | |
/// <summary> | |
/// If the previous task Canceled or Faulted, complete the master task with the same <see cref="Task.Status"/>. | |
/// Obtain the next <see cref="Task"/> from the <paramref name="taskEnumerator"/>. | |
/// If none, complete the master task, possibly with the <see cref="Task{T}.Result"/> of the last task. | |
/// Otherwise, set up the task with a continuation to come do this again when it completes. | |
/// </summary> | |
private static void ToTaskDoOneStep<TResult>( | |
IEnumerator<Task> taskEnumerator, TaskScheduler taskScheduler, | |
TaskCompletionSource<TResult> completionSource, Task completedTask) | |
{ | |
try | |
{ | |
// Check status of previous nested task (if any), and stop if Canceled or Faulted. | |
// In these cases, we are abandoning the enumerator, so we must dispose it. | |
TaskStatus status; | |
if (completedTask == null) | |
{ | |
// This is the first task from the iterator; skip status check. | |
} | |
else if ((status = completedTask.Status) == TaskStatus.Canceled) | |
{ | |
taskEnumerator.Dispose(); | |
completionSource.SetCanceled(); | |
return; | |
} | |
else if (status == TaskStatus.Faulted) | |
{ | |
taskEnumerator.Dispose(); | |
completionSource.SetException(completedTask.Exception.InnerExceptions); | |
return; | |
} | |
} | |
catch (Exception ex) | |
{ | |
// Return exception from disposing the enumerator. | |
completionSource.SetException(ex); | |
return; | |
} | |
// Find the next Task in the iterator; handle cancellation and other exceptions. | |
Boolean haveMore; | |
try | |
{ | |
// Enumerator disposes itself if it throws an exception or completes (returns false). | |
haveMore = taskEnumerator.MoveNext(); | |
} | |
catch (OperationCanceledException) | |
{ | |
//if (cancExc.CancellationToken == cancellationToken) completionSource.SetCanceled(); | |
//else completionSource.SetException(cancExc); | |
completionSource.SetCanceled(); | |
return; | |
} | |
catch (Exception exc) | |
{ | |
completionSource.SetException(exc); | |
return; | |
} | |
if (!haveMore) | |
{ | |
// No more tasks; set the result from the last completed task (if any, unless no result is requested). | |
// We know it's not Canceled or Faulted because we checked at the start of this method. | |
if (typeof(TResult) == typeof(VoidResult)) | |
{ // No result | |
completionSource.SetResult(default(TResult)); | |
} | |
else if (!(completedTask is Task<TResult>)) | |
{ // Wrong result | |
completionSource.SetException(new InvalidOperationException( | |
"Asynchronous iterator " + taskEnumerator + | |
" requires a final result task of type " + typeof(Task<TResult>).FullName + | |
(completedTask == null ? ", but none was provided." : | |
"; the actual task type was " + completedTask.GetType().FullName))); | |
} | |
else | |
{ | |
completionSource.SetResult(((Task<TResult>)completedTask).Result); | |
} | |
} | |
else | |
{ | |
// When the nested task completes, continue by performing this function again. | |
// Note: This is NOT a recursive call; the current method activation will complete | |
// almost immediately and independently of the lambda continuation. | |
taskEnumerator.Current.ContinueWith( | |
nextTask => ToTaskDoOneStep(taskEnumerator, taskScheduler, completionSource, nextTask), | |
taskScheduler); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment