Class AsyncActionFactory<TTuple>
Provides a way of invoking an Action delegate regardless of the amount of parameters provided.
public sealed class AsyncActionFactory<TTuple> : MutableTupleFactory<TTuple> where TTuple : MutableTuple
Type Parameters
TTupleThe type of the n-tuple representation of a MutableTuple.
- Inheritance
-
MutableTupleFactory<TTuple>AsyncActionFactory<TTuple>
- Inherited Members
Examples
AsyncActionFactory<T> encapsulates a deferred asynchronous action with typed arguments, with support for cloning for safe concurrent execution. This example creates a factory using AsyncActionFactory.Create<string, int> with a lambda that takes a channel name ("orders") and retry count (3), delays 10ms, and prints them, then calls ExecuteMethodAsync to run it. Key steps include cloning the factory via (AsyncActionFactory<MutableTuple<string, int>>)factory.Clone() and executing the clone separately. Console output displays "orders:3" for both the original and cloned execution, confirming that cloned factories produce identical results.
using System;
using System.Threading;
using System.Threading.Tasks;
using Cuemon;
using Cuemon.Threading;
namespace MyApp.Examples;
public static class AsyncActionFactoryExample
{
public static async Task DemonstrateAsync()
{
var factory = AsyncActionFactory.Create<string, int>(
async (channel, retryCount, cancellationToken) =>
{
await Task.Delay(10, cancellationToken).ConfigureAwait(false);
Console.WriteLine($"{channel}:{retryCount}");
},
"orders",
3);
await factory.ExecuteMethodAsync(CancellationToken.None);
var clone = (AsyncActionFactory<MutableTuple<string, int>>)factory.Clone();
await clone.ExecuteMethodAsync(CancellationToken.None);
}
}
Constructors
AsyncActionFactory(Func<TTuple, CancellationToken, Task>, TTuple)
Initializes a new instance of the AsyncActionFactory<TTuple> class.
public AsyncActionFactory(Func<TTuple, CancellationToken, Task> method, TTuple tuple)
Parameters
methodFunc<TTuple, CancellationToken, Task>The Task based function delegate to invoke.
tupleTTupleThe n-tuple argument of
method.
AsyncActionFactory(Func<TTuple, CancellationToken, Task>, TTuple, Delegate)
Initializes a new instance of the AsyncActionFactory<TTuple> class.
public AsyncActionFactory(Func<TTuple, CancellationToken, Task> method, TTuple tuple, Delegate originalDelegate)
Parameters
methodFunc<TTuple, CancellationToken, Task>The Task based 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 AsyncActionFactory<TTuple> object.
public override MutableTupleFactory<TTuple> Clone()
Returns
- MutableTupleFactory<TTuple>
A new AsyncActionFactory<TTuple> that is a copy of this instance.
Remarks
When thread safety is required this is the method to invoke.
ExecuteMethodAsync(CancellationToken)
Executes the delegate associated with this instance.
public Task ExecuteMethodAsync(CancellationToken ct)
Parameters
ctCancellationTokenThe token to monitor for cancellation requests. The default value is None.
Returns
- Task
A task that represents the asynchronous operation.
Exceptions
- InvalidOperationException
No delegate was specified on the factory.
- OperationCanceledException
The
ctwas canceled.