Class AsyncFuncFactory<TTuple, TResult>
Provides a way of invoking an Func<TResult> function delegate regardless of the amount of parameters provided.
public sealed class AsyncFuncFactory<TTuple, TResult> : MutableTupleFactory<TTuple> where TTuple : MutableTuple
Type Parameters
TTupleThe type of the n-tuple representation of a MutableTuple.
TResultThe type of the return value of the function delegate Cuemon.Threading.AsyncFuncFactory<TTuple, TResult>.Method.
- Inheritance
-
MutableTupleFactory<TTuple>AsyncFuncFactory<TTuple, TResult>
- Inherited Members
Examples
AsyncFuncFactory<TArg1, TArg2, TResult> encapsulates a deferred asynchronous function with typed arguments and return value, with cloning support for safe concurrent use. This example creates a factory using AsyncFuncFactory.Create with a lambda that takes "Hello" and "World" and returns their combined length, then calls ExecuteMethodAsync to compute the result. Key steps include checking HasDelegate and DelegateInfo properties, and cloning the factory via (AsyncFuncFactory<MutableTuple<string, string>, int>)factory.Clone() to execute independently. Console output confirms HasDelegate is True, the combined length is 10, and the cloned result is also 10.
using System;
using System.Threading;
using System.Threading.Tasks;
using Cuemon;
using Cuemon.Threading;
namespace MyApp.Examples
{
public class AsyncFuncFactoryExample
{
public async Task DemonstrateAsync()
{
// Create an AsyncFuncFactory that encapsulates a function taking
// two string arguments and returning an int.
var factory = AsyncFuncFactory.Create(
(string a, string b, CancellationToken ct) =>
{
ct.ThrowIfCancellationRequested();
return Task.FromResult(a.Length + b.Length);
},
"Hello",
"World"
);
Console.WriteLine($"Has delegate: {factory.HasDelegate}"); // True
Console.WriteLine($"Delegate info: {factory.DelegateInfo?.Name}"); // <Main>b__0_0 or similar
// Execute the wrapped function asynchronously.
var result = await factory.ExecuteMethodAsync(CancellationToken.None);
Console.WriteLine($"Total length: {result}"); // 10
// Clone the factory for safe concurrent use.
var clone = (AsyncFuncFactory<MutableTuple<string, string>, int>)factory.Clone();
var clonedResult = await clone.ExecuteMethodAsync(CancellationToken.None);
Console.WriteLine($"Cloned result: {clonedResult}"); // 10
}}
}
Constructors
AsyncFuncFactory(Func<TTuple, CancellationToken, Task<TResult>>, TTuple)
Initializes a new instance of the AsyncFuncFactory<TTuple, TResult> class.
public AsyncFuncFactory(Func<TTuple, CancellationToken, Task<TResult>> method, TTuple tuple)
Parameters
methodFunc<TTuple, CancellationToken, Task<TResult>>The function delegate to invoke.
tupleTTupleThe n-tuple argument of
method.
AsyncFuncFactory(Func<TTuple, CancellationToken, Task<TResult>>, TTuple, Delegate)
Initializes a new instance of the AsyncFuncFactory<TTuple, TResult> class.
public AsyncFuncFactory(Func<TTuple, CancellationToken, Task<TResult>> method, TTuple tuple, Delegate originalDelegate)
Parameters
methodFunc<TTuple, CancellationToken, Task<TResult>>The function delegate to invoke.
tupleTTupleThe n-tuple argument of
method.originalDelegateDelegateThe original delegate wrapped by
method.
Methods
Clone()
Creates a shallow copy of the current AsyncFuncFactory<TTuple, TResult> object.
public override MutableTupleFactory<TTuple> Clone()
Returns
- MutableTupleFactory<TTuple>
A new AsyncFuncFactory<TTuple, TResult> that is a copy of this instance.
Remarks
When thread safety is required this is the method to invoke.
ExecuteMethodAsync(CancellationToken)
Executes the function delegate associated with this instance.
public Task<TResult> ExecuteMethodAsync(CancellationToken ct)
Parameters
ctCancellationTokenThe token to monitor for cancellation requests. The default value is None.
Returns
- Task<TResult>
A task that represents the asynchronous operation. The task result contains the return value of the function delegate associated with this instance.
Exceptions
- InvalidOperationException
No delegate was specified on the factory.
- OperationCanceledException
The
ctwas canceled.