Table of Contents

Class AsyncHandlers

Namespace
Acuit.Pinpoint.Common.Threading
Assembly
Acuit.Pinpoint.Common.dll

Provides support for adding asynchronous handlers to a synchronous event mechanism.

public class AsyncHandlers
Inheritance
AsyncHandlers
Inherited Members

Remarks

This can be used with any synchronous event mechanism by adding an instance of this class as a property to the object raised with an event, which could be an EventArgs-derived class for regular .NET events, or a message class used by a messenger service.

Constructors

AsyncHandlers(CancellationToken)

Initializes a new instance of the AsyncHandlers class.

public AsyncHandlers(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

A cancellation token that can request cancellation of an asynchronous operation being performed by the event handler.

Properties

CancellationToken

Gets a cancellation token that can request cancellation of an asynchronous operation being performed by the event handler.

public CancellationToken CancellationToken { get; }

Property Value

CancellationToken

Task

Gets the Task representing asynchronous handling of the event, or null if none has been added.

public Task? Task { get; }

Property Value

Task

Methods

AddAsyncHandling(Func<CancellationToken, Task>)

Adds an asynchronous task to the event handler.

public void AddAsyncHandling(Func<CancellationToken, Task> handler)

Parameters

handler Func<CancellationToken, Task>

A function that will be invoked with the cancellation token and should return a task representing the asynchronous operation to be added.

Remarks

See the remarks for AddAsyncHandling(Task) for notes about asynchronous handlers.

Exceptions

ArgumentNullException

handler is null.

AddAsyncHandling(Task)

Adds an asynchronous task to the event handler.

public void AddAsyncHandling(Task handlerTask)

Parameters

handlerTask Task

The task representing the asynchronous operation to be added.

Remarks

Asynchronous handlers that are added effectively execute in parallel. However, a handler can optionally wait for previously-added handlers to complete first by, in its asynchronous handler code, awaiting the Task property (obtained before its asynchronous handler was added) if it is not null before performing its work.

Exceptions

ArgumentNullException

handlerTask is null.

Reset()

Resets this instance by clearing any previously-completed asynchronous handling task in Task.

public void Reset()

Remarks

This can be used to enable reuse of the same AsyncHandlers instance for multiple events.

CancellationToken will not be modified by this method; if a different cancellation token is needed, use the overload that accepts a cancellation token.

Exceptions

InvalidOperationException

There is a pending asynchronous handler that has not completed.

Reset(CancellationToken)

Resets this instance by clearing any previously-completed asynchronous handling task in Task and setting the cancellation token.

public void Reset(CancellationToken cancellationToken)

Parameters

cancellationToken CancellationToken

A cancellation token that can request cancellation of an asynchronous operation being performed by the event handler.

Remarks

This can be used to enable reuse of the same AsyncHandlers instance for multiple events.

Exceptions

InvalidOperationException

There is a pending asynchronous handler that has not completed.