An open API service indexing awesome lists of open source software.

https://github.com/dotnettools/eventextensions

An extension to .NET events to support asynchronous events and collect the returned values
https://github.com/dotnettools/eventextensions

async csharp dotnet events

Last synced: 10 months ago
JSON representation

An extension to .NET events to support asynchronous events and collect the returned values

Awesome Lists containing this project

README

          

# EventExtensions
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.

## What is exactly added?
- `InvokeAsync` to simply invoke async events
- `Collect` to collect the returned values by invoking synchronous events
- `CollectAsync` to invoke async events and collect the returned values
- `AsyncEventHandler` and `AsyncEventHandler` as the asynchronous versions of `EventHandler` and `EventHandler`

## Installation
Install via NuGet.

Install-Package EventExtensions -Version 2.2.0

## Getting Started

```csharp
using EventExtensions;
using System;

public class Program
{
public event Func> 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 SimpleHandler(int num)
{
await Task.Delay(TimeSpan.FromSeconds(1));
return num * 2;
}

public static Task Main(string[] args) {
return new Program().Run();
}
}
```