Class AsyncHandlers
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
cancellationTokenCancellationTokenA 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
Task
public Task? Task { get; }
Property Value
Methods
AddAsyncHandling(Func<CancellationToken, Task>)
Adds an asynchronous task to the event handler.
public void AddAsyncHandling(Func<CancellationToken, Task> handler)
Parameters
handlerFunc<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
handleris null.
AddAsyncHandling(Task)
Adds an asynchronous task to the event handler.
public void AddAsyncHandling(Task handlerTask)
Parameters
handlerTaskTaskThe 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
handlerTaskis 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
cancellationTokenCancellationTokenA 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.