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
- Host: GitHub
- URL: https://github.com/dotnettools/eventextensions
- Owner: dotnettools
- License: gpl-2.0
- Created: 2021-04-09T17:18:28.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2024-01-07T18:07:12.000Z (over 2 years ago)
- Last Synced: 2025-02-16T10:19:10.897Z (over 1 year ago)
- Topics: async, csharp, dotnet, events
- Language: C#
- Homepage:
- Size: 38.1 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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();
}
}
```