Class AsyncOptions
Specifies options that is related to asynchronous operations.
public class AsyncOptions : IAsyncOptions, IParameterObject
- Inheritance
-
AsyncOptions
- Implements
- Derived
Examples
The following example demonstrates how to configure AsyncOptions to provide a cancellation token for asynchronous operations.
using System;
using System.Threading;
using System.Threading.Tasks;
using Cuemon.Threading;
namespace Contoso.BackgroundJobs;
public sealed class AsyncOptionsExample
{
public static async Task RunAsync()
{
var defaults = new AsyncOptions();
Console.WriteLine(defaults.CancellationToken == CancellationToken.None);
using var cts = new CancellationTokenSource();
var options = new AsyncOptions
{
CancellationTokenProvider = () => cts.Token
};
cts.Cancel();
try
{
await Task.Delay(10, options.CancellationToken);
}
catch (OperationCanceledException)
{
Console.WriteLine("Cancelled as expected.");
}
}
}
Constructors
AsyncOptions()
Initializes a new instance of the AsyncOptions class.
public AsyncOptions()
Remarks
The following table shows the initial property values for an instance of AsyncOptions.
| Property | Initial Value |
|---|---|
| CancellationToken | default |
| CancellationTokenProvider | null |
Properties
CancellationToken
Gets or sets the cancellation token of an asynchronous operations.
public CancellationToken CancellationToken { get; set; }
Property Value
- CancellationToken
The cancellation token of an asynchronous operations.
Remarks
CancellationTokenProvider takes precedence when set, meaning that the getter of this property will invoke said mentioned function delegate.
CancellationTokenProvider
Gets or sets the function delegate that is invoked when a CancellationToken is requested.
public Func<CancellationToken> CancellationTokenProvider { get; set; }
Property Value
- Func<CancellationToken>
The function delegate that is invoked when a CancellationToken is requested.
Remarks
This function delegate is meant for edge cases where this instance might be stored as a singleton or similar use case.