From 292df67ea02dfb13916ff220200ba6d8bf8eed22 Mon Sep 17 00:00:00 2001 From: Brandon Minnick <13558917+brminnick@users.noreply.github.com> Date: Sun, 16 Dec 2018 12:52:15 -0800 Subject: [PATCH] Added WeakEventManager --- README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/README.md b/README.md index efdb431..e1eeee3 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ Extensions for `System.Threading.Tasks.Task`, inspired by [John Thiriet](https:/ - AsyncAwaitBestPractices - An extension method to safely fire-and-forget a `Task`: - `SafeFireAndForget` + - `WeakEventManager` - [Usage instructions below](#asyncawaitbestpractices) - AsyncAwaitBestPractices.MVVM - Allows for `Task` to safely be used asynchronously with `ICommand`: @@ -60,6 +61,21 @@ async Task ExampleAsyncMethod() } ``` +An event implementation that enables the [garbage collector to collect an object without needing to unsubscribe event handlers](http://paulstovell.com/blog/weakevents): +- `WeakEventManager` + +```csharp +readonly WeakEventManager _weakEventManager = new WeakEventManager(); + +public event EventHandler CanExecuteChanged +{ + add => _weakEventManager.AddEventHandler(value); + remove => _weakEventManager.RemoveEventHandler(value); +} + +public void RaiseCanExecuteChanged() => _weakEventManager.HandleEvent(this, EventArgs.Empty, nameof(CanExecuteChanged)); +``` + ### AsyncAwaitBestPractices.MVVM Allows for `Task` to safely be used asynchronously with `ICommand`: