Class AsyncPeriodicTask
Implements a common asynchronous coding pattern where an asynchronous operation is performed periodically and cancellation and exceptions are handled cleanly.
Namespace: Neon.Tasks
Assembly: Neon.Common.dll
Syntax
public sealed class AsyncPeriodicTask
Remarks
This class provides a nice way to implement the very common server side pattern where we need to periodically perform some operation until the operation signals that it's done or the task is canceled, while handling some of the messy exception handling details.
You'll use the AsyncPeriodicTask(TimeSpan, Func<Task<bool>>, Func<Exception, Task<bool>>, Func<Task>, CancellationTokenSource) constructor to create a task, passing the task interval, asynchronous task handler and optional exception handler and cancellation token and then call Run() to execute the task.
AsyncPeriodicTask will call the task handler, wait for the interval and then repeat. The task handler
return false
to continue running or true
to signal that AsyncPeriodicTask should stop.
AsyncPeriodicTask also monitors the cancellation token passed and watches for OperationCanceledException
thrown by the task handler to stop itself.
The exception handler will be called for all exceptions thrown by the task handler except for OperationCanceledException
exceptions as these signal that AsyncPeriodicTask should terminate. Exception handlers return false
to continue
running or true
to signal that AsyncPeriodicTask should stop.
stop.
Finally, an optional handler can be specified that will be called just before the AsyncPeriodicTask terminates.
note
This class implements IDisposable and the task will be terminated when this is called.
Constructors
AsyncPeriodicTask(TimeSpan, Func<Task<bool>>, Func<Exception, Task<bool>>, Func<Task>, CancellationTokenSource)
Constructs a periodic task.
Declaration
public AsyncPeriodicTask(TimeSpan interval, Func<Task<bool>> onTaskAsync, Func<Exception, Task<bool>> onExceptionAsync = null, Func<Task> onTerminateAsync = null, CancellationTokenSource cancellationTokenSource = null)
Parameters
Type | Name | Description |
---|---|---|
TimeSpan | interval | The interval between task executions. |
Func<Task<bool>> | onTaskAsync | Called periodically to implement the task. The callback should return |
Func<Exception, Task<bool>> | onExceptionAsync | Optional callback that handles exceptions thrown by the task. The callback should return |
Func<Task> | onTerminateAsync | Optional callback that will be called just before the task terminates. |
CancellationTokenSource | cancellationTokenSource | Optionally specifies the CancellationTokenSource that can be used to stop the task. |
Properties
CancellationTokenSource
Returns the cancellation token.
Declaration
public CancellationTokenSource CancellationTokenSource { get; }
Property Value
Type | Description |
---|---|
CancellationTokenSource |
Interval
Returns the task interval.
Declaration
public TimeSpan Interval { get; }
Property Value
Type | Description |
---|---|
TimeSpan |
Methods
Dispose()
Stops the task if it's running.
Declaration
public void Dispose()
Run()
Asynchronously executes the task until it exits or is canceled.
Declaration
public Task Run()
Returns
Type | Description |
---|---|
Task |