ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

Shorthand Task.Factory.FromAsync (for .NET 4.0)

This extension method series represent shorthand version of Task.Factory.FromAsync (for .NET 4.0)

Source

public static Task<TResult> ToTask<TResult>(
    this Func<TResult> function,
    AsyncCallback callback = default(AsyncCallback),
    object @object = default(object),
    TaskCreationOptions creationOptions = default(TaskCreationOptions),
    TaskScheduler scheduler = default(TaskScheduler))
{
    return Task<TResult>.Factory.FromAsync(
        function.BeginInvoke(callback, @object),
        function.EndInvoke, creationOptions,
        (scheduler ?? TaskScheduler.Current) ?? TaskScheduler.Default);
}

public static Task<TResult> ToTask<TArg1, TResult>(
    this Func<TArg1, TResult> function,
    TArg1 arg,
    AsyncCallback callback = default(AsyncCallback),
    object @object = default(object),
    TaskCreationOptions creationOptions = default(TaskCreationOptions))
{
    return Task<TResult>.Factory.FromAsync<TArg1>(
        function.BeginInvoke,
        function.EndInvoke,
        arg,
        @object, creationOptions);
}

public static Task<TResult> ToTask<TArg1, TArg2, TResult>(
    this Func<TArg1, TArg2, TResult> function,
    TArg1 arg1, TArg2 arg2,
    AsyncCallback callback = default(AsyncCallback),
    object @object = default(object),
    TaskCreationOptions creationOptions = default(TaskCreationOptions))
{
    return Task<TResult>.Factory.FromAsync<TArg1, TArg2>(
        function.BeginInvoke,
        function.EndInvoke,
        arg1, arg2,
        @object, creationOptions);
}

public static Task<TResult> ToTask<TArg1, TArg2, TArg3, TResult>(
    this Func<TArg1, TArg2, TArg3, TResult> function,
    TArg1 arg1, TArg2 arg2, TArg3 arg3,
    AsyncCallback callback = default(AsyncCallback),
    object @object = default(object),
    TaskCreationOptions creationOptions = default(TaskCreationOptions))
{
    return Task<TResult>.Factory.FromAsync<TArg1, TArg2, TArg3>(
        function.BeginInvoke,
        function.EndInvoke,
        arg1, arg2, arg3,
        @object, creationOptions);
}

public static Task ToTask(
    this Action action,
    AsyncCallback callback = default(AsyncCallback),
    object @object = default(object),
    TaskCreationOptions creationOptions = default(TaskCreationOptions),
    TaskScheduler scheduler = default(TaskScheduler))
{
    return Task.Factory.FromAsync(
        action.BeginInvoke(callback, @object),
        action.EndInvoke, creationOptions,
        (scheduler ?? TaskScheduler.Current) ?? TaskScheduler.Default);
}

public static Task ToTask<T>(
    this Action<T> action,
    T obj,
    AsyncCallback callback = default(AsyncCallback),
    object @object = default(object),
    TaskCreationOptions creationOptions = default(TaskCreationOptions),
    TaskScheduler scheduler = default(TaskScheduler))
{
    return Task.Factory.FromAsync(
        action.BeginInvoke(obj, callback, @object),
        action.EndInvoke, creationOptions,
        (scheduler ?? TaskScheduler.Current) ?? TaskScheduler.Default);
}

public static Task ToTask<T1, T2>(
    this Action<T1, T2> action,
    T1 arg1, T2 arg2,
    AsyncCallback callback = default(AsyncCallback),
    object @object = default(object),
    TaskCreationOptions creationOptions = default(TaskCreationOptions),
    TaskScheduler scheduler = default(TaskScheduler))
{
    return Task.Factory.FromAsync(
        action.BeginInvoke(arg1, arg2, callback, @object),
        action.EndInvoke, creationOptions,
        (scheduler ?? TaskScheduler.Current) ?? TaskScheduler.Default);
}

public static Task ToTask<T1, T2, T3>(
    this Action<T1, T2, T3> action,
    T1 arg1, T2 arg2, T3 arg3,
    AsyncCallback callback = default(AsyncCallback),
    object @object = default(object),
    TaskCreationOptions creationOptions = default(TaskCreationOptions),
    TaskScheduler scheduler = default(TaskScheduler))
{
    return Task.Factory.FromAsync(
        action.BeginInvoke(arg1, arg2, arg3, callback, @object),
        action.EndInvoke, creationOptions,
        (scheduler ?? TaskScheduler.Current) ?? TaskScheduler.Default);
}

public static Task ToTask<T1, T2, T3, T4>(
    this Action<T1, T2, T3, T4> action,
    T1 arg1, T2 arg2, T3 arg3, T4 arg4,
    AsyncCallback callback = default(AsyncCallback),
    object @object = default(object),
    TaskCreationOptions creationOptions = default(TaskCreationOptions),
    TaskScheduler scheduler = default(TaskScheduler))
{
    return Task.Factory.FromAsync(
        action.BeginInvoke(arg1, arg2, arg3, arg4, callback, @object),
        action.EndInvoke, creationOptions,
        (scheduler ?? TaskScheduler.Current) ?? TaskScheduler.Default);
}

public static Task ToTask<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 = default(AsyncCallback),
    object @object = default(object),
    TaskCreationOptions creationOptions = default(TaskCreationOptions),
    TaskScheduler scheduler = default(TaskScheduler))
{
    return Task.Factory.FromAsync(
        action.BeginInvoke(arg1, arg2, arg3, arg4, arg5, callback, @object),
        action.EndInvoke, creationOptions,
        (scheduler ?? TaskScheduler.Current) ?? TaskScheduler.Default);
}

public static Task ToTask<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 = default(AsyncCallback),
    object @object = default(object),
    TaskCreationOptions creationOptions = default(TaskCreationOptions),
    TaskScheduler scheduler = default(TaskScheduler))
{
    return Task.Factory.FromAsync(
        action.BeginInvoke(arg1, arg2, arg3, arg4, arg5, arg6, callback, @object),
        action.EndInvoke, creationOptions,
        (scheduler ?? TaskScheduler.Current) ?? TaskScheduler.Default);
}

public static Task ToTask<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 = default(AsyncCallback),
    object @object = default(object),
    TaskCreationOptions creationOptions = default(TaskCreationOptions),
    TaskScheduler scheduler = default(TaskScheduler))
{
    return Task.Factory.FromAsync(
        action.BeginInvoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7, callback, @object),
        action.EndInvoke, creationOptions,
        (scheduler ?? TaskScheduler.Current) ?? TaskScheduler.Default);
}

public static Task ToTask<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 = default(AsyncCallback),
    object @object = default(object),
    TaskCreationOptions creationOptions = default(TaskCreationOptions),
    TaskScheduler scheduler = default(TaskScheduler))
{
    return Task.Factory.FromAsync(
        action.BeginInvoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, callback, @object),
        action.EndInvoke, creationOptions,
        (scheduler ?? TaskScheduler.Current) ?? TaskScheduler.Default);
}

public static Task ToTask<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 = default(AsyncCallback),
    object @object = default(object),
    TaskCreationOptions creationOptions = default(TaskCreationOptions),
    TaskScheduler scheduler = default(TaskScheduler))
{
    return Task.Factory.FromAsync(
        action.BeginInvoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, callback, @object),
        action.EndInvoke, creationOptions,
        (scheduler ?? TaskScheduler.Current) ?? TaskScheduler.Default);
}

public static Task ToTask<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 = default(AsyncCallback),
    object @object = default(object),
    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, @object),
        action.EndInvoke, creationOptions,
        (scheduler ?? TaskScheduler.Current) ?? TaskScheduler.Default);
}

public static Task ToTask<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 = default(AsyncCallback),
    object @object = default(object),
    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, @object),
        action.EndInvoke, creationOptions,
        (scheduler ?? TaskScheduler.Current) ?? TaskScheduler.Default);
}

public static Task ToTask<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 = default(AsyncCallback),
    object @object = default(object),
    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, @object),
        action.EndInvoke, creationOptions,
        (scheduler ?? TaskScheduler.Current) ?? TaskScheduler.Default);
}

public static Task ToTask<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 = default(AsyncCallback),
    object @object = default(object),
    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, @object),
        action.EndInvoke, creationOptions,
        (scheduler ?? TaskScheduler.Current) ?? TaskScheduler.Default);
}

public static Task ToTask<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 = default(AsyncCallback),
    object @object = default(object),
    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, @object),
        action.EndInvoke, creationOptions,
        (scheduler ?? TaskScheduler.Current) ?? TaskScheduler.Default);
}

public static Task ToTask<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 = default(AsyncCallback),
    object @object = default(object),
    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, @object),
        action.EndInvoke, creationOptions,
        (scheduler ?? TaskScheduler.Current) ?? TaskScheduler.Default);
}

public static Task ToTask<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 = default(AsyncCallback),
    object @object = default(object),
    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, @object),
        action.EndInvoke, creationOptions,
        (scheduler ?? TaskScheduler.Current) ?? TaskScheduler.Default);
}

Example

((Func<double, double, double>)Math.Pow)
    .ToTask(2d, 2d)
    .ContinueWith(x => ((Action<string, object[]>)Console.WriteLine).ToTask("Power value: {0}", new object[] { x.Result }))
    .Wait();
Console.WriteLine("Program completed.");
Console.ReadLine();