15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

events ❘ 203<br />

Weak events<br />

With events, the publisher <strong>and</strong> listener are directly connected. This can be a problem with garbage<br />

collection. For example, if a listener is not directly referenced any more, there’s still a reference from the<br />

publisher. The garbage collector cannot clean up memory from the listener, as the publisher still holds a<br />

reference <strong>and</strong> fires events to the listener.<br />

This strong connection can be resolved by using the weak event pattern <strong>and</strong> using the WeakEventManager<br />

as an intermediate between the publisher <strong>and</strong> listeners.<br />

The sample from before with the CarDealer as publisher <strong>and</strong> Consumer as listener is modified in the<br />

following section to use the weak event pattern.<br />

Weak event Manager<br />

To use weak events you need to create a class that derives from WeakEventManager. WeakEventManager is<br />

defined in the namespace System.Windows in the assembly WindowsBase.<br />

The class WeakCarInfoEventManager is the weak event manager class that manages the connection<br />

between the publisher <strong>and</strong> listener for the NewCarInfo event. This class implements a singleton pattern<br />

so that only one instance is created. The static property CurrentManager creates an object of type<br />

WeakCarInfoEventManager if it doesn’t exist, <strong>and</strong> returns a reference to it. WeakCarInfoEventManager.<br />

CurrentManager is used to access the singleton object from the WeakCarInfoEventManager.<br />

With the weak event pattern, the weak event manager class needs static methods AddListener() <strong>and</strong><br />

RemoveListener(). The listener is connected <strong>and</strong> disconnected to the events of the publisher with these<br />

methods instead of using the events from the publisher directly. The listener also needs to implement the<br />

interface IWeakEventListener that is shown shortly. With the AddListener() <strong>and</strong> RemoveListener()<br />

methods, methods from the base class WeakEventManager are invoked to add <strong>and</strong> remove the listeners.<br />

With the WeakCarInfoEventManager class you also need to override the StartListening() <strong>and</strong><br />

StopListening() methods from the base class. StartListening() is called when the first listener is<br />

added, StopListening() when the last listener is removed. StartListening() <strong>and</strong> StopListening()<br />

subscribes <strong>and</strong> unsubscribes a method from the weak event manager to listen for the event from the<br />

publisher. In case the weak event manager class needs to connect to different publisher types, you can<br />

check the type information from the source object before doing the cast. The event is then forwarded to<br />

the listeners by calling the DeliverEvent() method from the base class, which in turn invokes the method<br />

ReceiveWeakEvent() from the IWeakEventListener interface in the listeners:<br />

using System.Windows;<br />

namespace Wrox.ProCSharp.Delegates<br />

{<br />

public class WeakCarInfoEventManager: WeakEventManager<br />

{<br />

public static void AddListener(object source, IWeakEventListener listener)<br />

{<br />

CurrentManager.ProtectedAddListener(source, listener);<br />

}<br />

public static void RemoveListener(object source,<br />

IWeakEventListener listener)<br />

{<br />

CurrentManager.ProtectedRemoveListener(source, listener);<br />

}<br />

public static WeakCarInfoEventManager CurrentManager<br />

{<br />

get<br />

{<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!