Using the WeakEvent pattern with WeakEventManager

Normal event handlers are defined using the += operator and the source keeps a reference to its listeners. If this reference is not removed, it prevents the listener from receiving garbage data. This is one of the most common causes of memory leaks, which are now much easier to avoid.

WPF 4.5 WeakEventManager provides us with a central event dispatching capability that allows the listeners to be de-allocated from memory, while the event is still alive.

While this is not a strictly new behavior, WPF 4.5 brings us enhanced support to set up a weak reference to an event. Prior to this version of WPF, we had to create a weak event manager for every event. This is not the case anymore. Now we can use ...

Get Microsoft .NET Framework 4.5 Quickstart Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.