Table of Contents

AsyncPatterns Class

Definition

Namespace
Cuemon.Threading
Assemblies
Cuemon.Threading.dll
Source
src/Cuemon.Threading/AsyncPatterns.cs

Provides a generic way to support different types of design patterns and practices with small utility methods scoped to Task.

public sealed class AsyncPatterns
Inheritance
AsyncPatterns

Examples

AsyncPatterns provides safe asynchronous patterns for resource management, including SafeInvokeAsync that ensures proper disposal of IDisposable resources (CA2000 compliant). This example calls SafeInvokeAsync with a factory delegate creating a MemoryStream, an invocation delegate that writes "Cuemon" bytes to it and returns the stream, and a CancellationToken. The resulting stream is read back via StreamReader to confirm the content, and the AsyncPatterns.Use sentinel is compared with itself to verify static reference identity. Console output displays "Cuemon" and True for the reference comparison.

using System;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Cuemon.Threading;

namespace Contoso.Streaming;

public sealed class AsyncPatternsExample
{
    public static async Task RunAsync()
    {
        var result = await AsyncPatterns.SafeInvokeAsync(
            () => new MemoryStream(),
            async (stream, ct) =>
            {
                byte[] buffer = Encoding.UTF8.GetBytes("Cuemon");
                await stream.WriteAsync(buffer, 0, buffer.Length, ct);
                stream.Position = 0;
                return stream;
            },
            ct: CancellationToken.None);

        using (result)
        using (var reader = new StreamReader(result, Encoding.UTF8, true, 1024, true))
        {
            Console.WriteLine(await reader.ReadToEndAsync());
        }

        Console.WriteLine(ReferenceEquals(AsyncPatterns.Use, AsyncPatterns.Use));
    }
}

Properties

Name Description
Use

Gets the singleton instance of the AsyncPatterns functionality allowing for extensions methods like: AsyncPatterns.Use.SomeIngeniousMethod().

Methods

Name Description
SafeInvokeAsync<TResult>(Func<TResult>, Func<TResult, CancellationToken, Task<TResult>>, Func<Exception, CancellationToken, Task>, CancellationToken)

Provides a generic way to abide the rule description of CA2000 (Dispose objects before losing scope).

SafeInvokeAsync<T, TResult>(Func<TResult>, Func<TResult, T, CancellationToken, Task<TResult>>, T, Func<Exception, T, CancellationToken, Task>, CancellationToken)

Provides a generic way to abide the rule description of CA2000 (Dispose objects before losing scope).

SafeInvokeAsync<T1, T2, TResult>(Func<TResult>, Func<TResult, T1, T2, CancellationToken, Task<TResult>>, T1, T2, Func<Exception, T1, T2, CancellationToken, Task>, CancellationToken)

Provides a generic way to abide the rule description of CA2000 (Dispose objects before losing scope).

SafeInvokeAsync<T1, T2, T3, TResult>(Func<TResult>, Func<TResult, T1, T2, T3, CancellationToken, Task<TResult>>, T1, T2, T3, Func<Exception, T1, T2, T3, CancellationToken, Task>, CancellationToken)

Provides a generic way to abide the rule description of CA2000 (Dispose objects before losing scope).

SafeInvokeAsync<T1, T2, T3, T4, TResult>(Func<TResult>, Func<TResult, T1, T2, T3, T4, CancellationToken, Task<TResult>>, T1, T2, T3, T4, Func<Exception, T1, T2, T3, T4, CancellationToken, Task>, CancellationToken)

Provides a generic way to abide the rule description of CA2000 (Dispose objects before losing scope).

SafeInvokeAsync<T1, T2, T3, T4, T5, TResult>(Func<TResult>, Func<TResult, T1, T2, T3, T4, T5, CancellationToken, Task<TResult>>, T1, T2, T3, T4, T5, Func<Exception, T1, T2, T3, T4, T5, CancellationToken, Task>, CancellationToken)

Provides a generic way to abide the rule description of CA2000 (Dispose objects before losing scope).