Table of Contents

AsyncFuncFactory Class

Definition

Namespace
Cuemon.Threading
Assemblies
Cuemon.Threading.dll
Source
src/Cuemon.Threading/AsyncFuncFactory.cs

Provides access to factory methods for creating AsyncFuncFactory<TTuple, TResult> instances that encapsulate a Task<TResult> based function delegate with a variable amount of generic arguments.

public static class AsyncFuncFactory
Inheritance
AsyncFuncFactory

Examples

AsyncFuncFactory provides factory methods for creating deferred asynchronous functions with zero or more typed arguments and a return value, executed via ExecuteMethodAsync. This example creates a function that returns 42 and another that sums two integers (3 and 4) after a 10ms delay, then invokes each with CancellationToken.None. Key setup includes using AsyncFuncFactory.Create with a lambda and passing arguments separately so the factory manages lifetime and error propagation. Console output displays "Result: 42" and "Sum: 7", confirming both the parameterless and parameterized async function patterns.

using System;
using System.Threading;
using System.Threading.Tasks;
using Cuemon.Threading;

namespace Cuemon.Threading;

public class AsyncFuncFactoryExample
{
    public async Task DemonstrateAsync()
    {
        var factory = AsyncFuncFactory.Create(ct =>
        {
            return Task.FromResult(42);
        });
        var result = await factory.ExecuteMethodAsync(CancellationToken.None);
        Console.WriteLine($"Result: {result}");

        var factoryWithArg = AsyncFuncFactory.Create(async (int a, int b, CancellationToken ct) =>
        {
            await Task.Delay(10, ct);
            return a + b;
        }, 3, 4);
        var sum = await factoryWithArg.ExecuteMethodAsync(CancellationToken.None);
        Console.WriteLine($"Sum: {sum}");
    }
}

Methods

Name Description
Create<TResult>(Func<CancellationToken, Task<TResult>>)

Creates a new AsyncFuncFactory<TTuple, TResult> instance encapsulating the specified method.

Create<T1, T2, T3, T4, T5, T6, T7, T8, T9, TResult>(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, CancellationToken, Task<TResult>>, T1, T2, T3, T4, T5, T6, T7, T8, T9)

Creates a new AsyncFuncFactory<TTuple, TResult> instance encapsulating the specified method and nine generic arguments.

Create<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, TResult>(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, CancellationToken, Task<TResult>>, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)

Creates a new AsyncFuncFactory<TTuple, TResult> instance encapsulating the specified method and ten generic arguments.

Create<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, TResult>(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, CancellationToken, Task<TResult>>, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)

Creates a new AsyncFuncFactory<TTuple, TResult> instance encapsulating the specified method and eleven generic arguments.

Create<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, TResult>(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, CancellationToken, Task<TResult>>, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12)

Creates a new AsyncFuncFactory<TTuple, TResult> instance encapsulating the specified method and twelfth generic arguments.

Create<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, TResult>(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, CancellationToken, Task<TResult>>, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13)

Creates a new AsyncFuncFactory<TTuple, TResult> instance encapsulating the specified method and thirteen generic arguments.

Create<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, TResult>(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, CancellationToken, Task<TResult>>, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14)

Creates a new AsyncFuncFactory<TTuple, TResult> instance encapsulating the specified method and fourteen generic arguments.

Create<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, TResult>(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, CancellationToken, Task<TResult>>, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15)

Creates a new AsyncFuncFactory<TTuple, TResult> instance encapsulating the specified method and fifteen generic arguments.

Create<T, TResult>(Func<T, CancellationToken, Task<TResult>>, T)

Creates a new AsyncFuncFactory<TTuple, TResult> instance encapsulating the specified method and one generic argument.

Create<T1, T2, TResult>(Func<T1, T2, CancellationToken, Task<TResult>>, T1, T2)

Creates a new AsyncFuncFactory<TTuple, TResult> instance encapsulating the specified method and two generic arguments.

Create<T1, T2, T3, TResult>(Func<T1, T2, T3, CancellationToken, Task<TResult>>, T1, T2, T3)

Creates a new AsyncFuncFactory<TTuple, TResult> instance encapsulating the specified method and three generic arguments.

Create<T1, T2, T3, T4, TResult>(Func<T1, T2, T3, T4, CancellationToken, Task<TResult>>, T1, T2, T3, T4)

Creates a new AsyncFuncFactory<TTuple, TResult> instance encapsulating the specified method and four generic arguments.

Create<T1, T2, T3, T4, T5, TResult>(Func<T1, T2, T3, T4, T5, CancellationToken, Task<TResult>>, T1, T2, T3, T4, T5)

Creates a new AsyncFuncFactory<TTuple, TResult> instance encapsulating the specified method and five generic arguments.

Create<T1, T2, T3, T4, T5, T6, TResult>(Func<T1, T2, T3, T4, T5, T6, CancellationToken, Task<TResult>>, T1, T2, T3, T4, T5, T6)

Creates a new AsyncFuncFactory<TTuple, TResult> instance encapsulating the specified method and six generic arguments.

Create<T1, T2, T3, T4, T5, T6, T7, TResult>(Func<T1, T2, T3, T4, T5, T6, T7, CancellationToken, Task<TResult>>, T1, T2, T3, T4, T5, T6, T7)

Creates a new AsyncFuncFactory<TTuple, TResult> instance encapsulating the specified method and seven generic arguments.

Create<T1, T2, T3, T4, T5, T6, T7, T8, TResult>(Func<T1, T2, T3, T4, T5, T6, T7, T8, CancellationToken, Task<TResult>>, T1, T2, T3, T4, T5, T6, T7, T8)

Creates a new AsyncFuncFactory<TTuple, TResult> instance encapsulating the specified method and eight generic arguments.