ChannelExecutor
Sequential operation executor based on channels. Ensures operations are executed sequentially to prevent file access conflicts.
Inherits: AsyncDisposable
Implements: IChannelExecutorGroup
Constructors
public ChannelExecutor(Action<Exception> errorHandler, TimeSpan flushInterval)
channelExecutor = ChannelExecutor(errorHandler, flushInterval)
Initializes a new instance of the ChannelExecutor.
- errorHandler
- Error handler for unhandled exceptions.
- flushInterval
- Interval for batch processing. Use TimeSpan.Zero for immediate execution.
Methods
public void Add(Func<CancellationToken, ValueTask> action)
channelExecutor.Add(action)
Add async operation to the execution queue.
- action
- Async operation to execute (receives CancellationToken).
public Task AddAndWaitAsync(Func<CancellationToken, ValueTask> action, CancellationToken cancellationToken)
result = channelExecutor.AddAndWaitAsync(action, cancellationToken)
Add async operation to the execution queue and wait for it to complete.
- action
- Async operation to execute (receives CancellationToken).
- cancellationToken
- Cancellation token.
Returns: Task that completes when the operation has been executed.
public ValueTask AddAsync(Func<CancellationToken, ValueTask> action, CancellationToken cancellationToken)
result = channelExecutor.AddAsync(action, cancellationToken)
Add async operation to the execution queue asynchronously.
- action
- Async operation to execute (receives CancellationToken).
- cancellationToken
- Cancellation token.
Returns: Task.
public IChannelExecutorGroup CreateGroup(Func<CancellationToken, ValueTask> begin, Func<CancellationToken, ValueTask> end)
result = channelExecutor.CreateGroup(begin, end)
Creates a new operation group with async begin/end callbacks.
- begin
- Async action called before first operation in the group (receives CancellationToken).
- end
- Async action called after last operation in the group (always, like finally; receives CancellationToken).
Returns: Group object to add operations to.
protected override ValueTask DisposeManaged()
result = channelExecutor.DisposeManaged()
Disposes the managed resources asynchronously. Override this method to add custom clean up of managed resources.
public Task RunAsync(CancellationToken cancellationToken)
result = channelExecutor.RunAsync(cancellationToken)
Starts the channel processor.
- cancellationToken
- External cancellation token to stop processing.
Returns: Task that completes when processing is stopped.
public Task WaitFlushAsync(CancellationToken cancellationToken)
result = channelExecutor.WaitFlushAsync(cancellationToken)
Wait for all pending operations to complete.
- cancellationToken
- Cancellation token.
Returns: Task.