Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

WPF 4.5 – Part 2 : Improved WeakEventManager

DZone's Guide to

WPF 4.5 – Part 2 : Improved WeakEventManager

·
Free Resource

weakEventMemory leaks were, are and always will be a concern in an application. One of it’s classical origin is unsubscribed events handler.

The weak event pattern is here to the rescue but it is quite tedious to implement.

In this post we’ll see how the WPF teams ease up our life when using the WeakEventManager class.

This post is a part of a series about the new features of WPF 4.5.

Generic Weak Event Manager

Prior to WPF 4.5 you had too create a weak event manger for every event you want to subscribe too.

Now it’s over and you can use a generic version of the WeakEventManager class.

It takes as a generic parameters the type of the event’s source and the type of the dealed event arguments.

// Type parameters:
//   TEventSource:
//     The type that raises the event.
//
//   TEventArgs:
//     The type that holds the event data.
public class WeakEventManager<TEventSource, TEventArgs> :
             WeakEventManager where TEventArgs : EventArgs

It also exposes two statics methods on it:

  • AddHandler to add an handler on a event of a given source. It takes the name of the event as a parameters;
  • RemoveHandler to remove an handler if you know when;

It is done using reflection so you can have a little performance overhead using this object.

No more interface for the subscriber

Prior to WPF 4.5, every subscriber to a weak-event must implements the IWeakEventListener. This is a very simple interface with this declaration:

public bool ReceiveWeakEvent(Type managerType, object sender, EventArgs e)

Even if it’s easy and fast to implement, it is quite tedious. Hopefully, implementing it is no more needed and you simply have to pass on a delegate when subscribing !.

An example

Let’s said you have an application with a main window and sometimes you show up child ones. This children subscribe to the Activated event of the main window to know when the application is showed up. By using traditional event subscription, you could create a memory leak if you do not register from it. In the other hand, if you subscribe to this event using a WeakEventManager, you will never heard of it!

Here is an example of the code to use:

//Create 10 Mo to be more visible in the process explorer
public byte[] data = new byte[10 * 1024 * 1024];
 
public LeakingWindow()
{
    InitializeComponent();
    WeakEventManager<Window, EventArgs>
        .AddHandler(App.Current.MainWindow, "Activated", MainWindow_Activated);
 
    //Traditional event subscription: memory leak !
    App.Current.MainWindow.Activated += MainWindow_Activated;
}
 
void MainWindow_Activated(object sender, EventArgs e)
{
    //Do something here
}

To test this feature, I created a little demo app which create leaking window and dump the memory usage every 200ms. You can find this application on my dropbox folder (do you want to register and give me more space ? use this link !!)

For more informations on this topic, read this MSDN page.

Topics:

Published at DZone with permission of Jon Antoine, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}