This library adds a few extension methods to events. While it's not best practice to have async events, this library attempts to provide the following features:
- Collect the returned values of event handlers synchronously or asynchronously.
- Invoke an event asynchronously.
- Handles NULL events; since it checks for null values inside the extension methods and thus throws no null-pointer exceptions.
- InvokeAsyncto simply invoke async events
- Collectto collect the returned values by invoking synchronous events
- CollectAsyncto invoke async events and collect the returned values
- AsyncEventHandlerand- AsyncEventHandler<T>as the asynchronous versions of- EventHandlerand- EventHandler<T>
Install via NuGet.
Install-Package EventExtensions -Version 2.2.0
using EventExtensions;
using System;
public class Program
{
    public event Func<int, Task<int>> MyEvent;
    public async Task Run() {
        MyEvent += SimpleHandler;
        var results = await MyEvent.CollectAsync(8); // There are other flavors to this method such as 'InvokeAsync'!
        foreach (var result in results)
            Console.WriteLine(result);
    }
    private async Task<int> SimpleHandler(int num)
    {
        await Task.Delay(TimeSpan.FromSeconds(1));
        return num * 2;
    }
    public static Task Main(string[] args) {
        return new Program().Run();
    }
}