WeakEventManager Class (System.Windows)

Switch View :
ScriptFree
.NET Framework Class Library
WeakEventManager Class

Provides a base class for the event manager that is used in the weak event pattern. The manager adds and removes listeners for events (or callbacks) that also use the pattern.

Inheritance Hierarchy

System.Object
  System.Windows.Threading.DispatcherObject
    System.Windows.WeakEventManager
      System.Collections.Specialized.CollectionChangedEventManager
      System.ComponentModel.CurrentChangedEventManager
      System.ComponentModel.CurrentChangingEventManager
      System.ComponentModel.PropertyChangedEventManager
      System.Windows.Data.DataChangedEventManager
      System.Windows.LostFocusEventManager

Namespace:  System.Windows
Assembly:  WindowsBase (in WindowsBase.dll)
Syntax

Visual Basic
Public MustInherit Class WeakEventManager _
	Inherits DispatcherObject
C#
public abstract class WeakEventManager : DispatcherObject
Visual C++
public ref class WeakEventManager abstract : public DispatcherObject
F#
[<AbstractClass>]
type WeakEventManager =  
    class
        inherit DispatcherObject
    end

The WeakEventManager type exposes the following members.

Constructors

  Name Description
Protected method WeakEventManager Initializes base class values when it is used as the initializer by the constructor of a derived class.
Top
Properties

  Name Description
Public property Dispatcher Gets the Dispatcher this DispatcherObject is associated with. (Inherited from DispatcherObject.)
Protected property Item Gets or sets the data being stored for the specified source.
Protected property ReadLock Establishes a read-lock on the underlying data table, and returns an IDisposable.
Protected property WriteLock Establishes a write-lock on the underlying data table, and returns an IDisposable.
Top
Methods

  Name Description
Public method CheckAccess Determines whether the calling thread has access to this DispatcherObject. (Inherited from DispatcherObject.)
Protected method DeliverEvent Delivers the event being managed to each listener.
Protected method DeliverEventToList Delivers the event being managed to each listener in the provided list.
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Protected method Static member GetCurrentManager Returns the WeakEventManager implementation that is used for the provided type.
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Protected method ProtectedAddListener Adds the provided listener to the provided source for the event being managed.
Protected method ProtectedRemoveListener Removes a previously added listener from the provided source.
Protected method Purge Removes inactive listener entries from the data list for the provided source. Returns true if some entries were actually removed from the list.
Protected method Remove Removes all listeners for the specified source.
Protected method ScheduleCleanup Requests that a purge of unused entries in the underlying listener list be performed on a lower priority thread.
Protected method Static member SetCurrentManager Sets the current manager for the specified manager type.
Protected method StartListening When overridden in a derived class, starts listening for the event being managed. After the StartListening method is first called, the manager should be in the state of calling DeliverEvent or DeliverEventToList whenever the relevant event from the provided source is handled.
Protected method StopListening When overridden in a derived class, stops listening on the provided source for the event being managed.
Public method ToString Returns a string that represents the current object. (Inherited from Object.)
Public method VerifyAccess Enforces that the calling thread has access to this DispatcherObject. (Inherited from DispatcherObject.)
Top
Remarks

You typically use the weak event pattern when the event source has an object lifetime that is independent of the event listeners. Using the central event dispatching capability of a WeakEventManager allows the listener's handlers to be garbage collected even if the source object persists. By contrast, a regular event hookup using the += operator causes the potentially disconnected source to hold a reference to the listeners. This prevents the receiver from being garbage collected in a timely fashion.

One common situation where the lifetime relationships between sources and listeners should use of the weak event pattern is the handling of update events coming from sources for data bindings.

The weak event pattern can also be used for callbacks and regular events.

Notes to Inheritors

Classes that derive from WeakEventManager class should do the following:

  • Provide a static AddListener method. Sources call AddListener to add a listener for the managed weak event. Your implementation calls the ProtectedAddListener method to implement this behavior.

  • Provide a static RemoveListener method. Sources call RemoveListener to add a listener for the managed event. Your implementation calls the ProtectedRemoveListener method to implement this behavior.

  • Override the StartListening method to cast the source to the type that owns the event, and connect the handler on the source to the event being managed.

  • Override the StopListening method to cast the source to the type that owns the event, and disconnect the handler on the source to the event being managed.

  • Implement the handler, which should call the DeliverEvent event, so that the managed event is forwarded to its weak event pattern listeners.

  • Provide a CurrentManager property that returns the specific manager type being implemented. The get accessor for CurrentManager should call the GetCurrentManager method to make sure that there is not already an initialized instance. If so, that instance is returned properly typed. If there is no initialized instance, the get accessor should call the SetCurrentManager method to initialize one.

Version Information

.NET Framework

Supported in: 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Platforms

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
See Also

Reference

Other Resources

Community Content

quetzalcoatl.pl
Source-side only?
Either I misread something, or the WeakEventManager is a base class for those who want to implement a "weak event". I believe that the descriptions for AddListener points that. But how do we add a WEAK LISTENERS to them? Most of the time, I want to make  one specific listener weak, while all others may stay (or even should) be left normal. Also, how do we "weaken" all the existing events all around the framework? IMHO, the handlers should be optionally weakened-able, and that would solve almost everything

Shimmy Weitzhandler
POOR DOCUMENTATION
Except for the poor documentation that doesn't give any sample code, I think there should be an already implemented WeakEventManager + WeakEventManager<TEvent> that does it all.

claudiumihaila
Code example please ...
A code example how to derive from WeakEventManager wouldn't hurt.

LukePuplett
CurrentManager should be static
Documentation bug.

Swodog
RemoveListener() Documentation Needs Editing
The documentation needs to be edited for clarity. As it stands now, the static RemoveListener() method is supposed to do the exact same thing as the AddListener() method. I believe the text should read something more along these lines: "...the static RemoveListener removes a listener from the event..." $0$0 $0