https://github.com/tibel/deferredevents
A simple .NET Standard 2.0 implementation for deferred events
https://github.com/tibel/deferredevents
deferred-events netstandard20
Last synced: 3 months ago
JSON representation
A simple .NET Standard 2.0 implementation for deferred events
- Host: GitHub
- URL: https://github.com/tibel/deferredevents
- Owner: tibel
- License: mit
- Created: 2019-06-15T10:46:19.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-04-12T07:54:40.000Z (about 5 years ago)
- Last Synced: 2025-01-10T01:47:26.839Z (5 months ago)
- Topics: deferred-events, netstandard20
- Language: C#
- Size: 26.4 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Deferred Events
This repository contains a .NET Standard 2.0 implementation for Deferred Events.
Deferred Events allows event invocators to await for the asynchronous completion of all event handlers.
## Installation
Install the NuGet package by running the following command:
```
Install-Package DeferredEvents
```## Usage
Start by adding the namespace to your code files:
```csharp
using DeferredEvents;
```Change your events signature so that they can use `DeferredEventArgs` instead of `EventArgs`:
```csharp
// before
public event EventHandler MyEvent;
``````csharp
// after
public event EventHandler MyEvent;
```If you have a custom event arguments class inheriting from `EventArgs`, just make it inherit from `DeferredEventArgs` instead.
Last step is to change the way you make your invocations:
```csharp
// before
MyEvent?.Invoke(sender, EventArgs.Empty);
``````csharp
// after
await MyEvent.InvokeAsync(sender, new DeferredEventArgs());
```The `InvokeAsync()` method is an extension method that will enable you to ensure we wait for the event handlers to finish their work before we proceed.
Last step will be to change the event handlers code so it can take advantage of the deferred execution:
```csharp
// before
public void OnMyEvent(object sender, EventArgs e)
{
// code
}
``````csharp
// after
public async void OnMyEvent(object sender, DeferredEventArgs e)
{
var deferral = e.GetDeferral();
// awaiteable code
deferral.Complete();
}
```You can also use the `using` pattern if you prefer:
```csharp
// after
public async void OnMyEvent(object sender, DeferredEventArgs e)
{
using (e.GetDeferral())
{
// awaiteable code
}
}
```You only need to call `e.GetDeferral()` if you actually want to the event caller to wait for the completion of the event handler; if you don't call it, it will just behave as a regular event handler.
You **must** call `e.GetDeferral()` to get an `EventDeferral` instance before any `await` call in your code to ensure that the event caller knows that it should wait for `deferral.Complete()`; ideally, it should be the first thing you do in the event handler code.
If you have indeed called `e.GetDeferral()`, then you **must** call `deferral.Complete()` to signal that the event handler has finished.