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.

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

[<AbstractClass>]
type WeakEventManager =  
    class 
        inherit DispatcherObject 
    end

The WeakEventManager type exposes the following members.

  NameDescription
Protected methodWeakEventManagerInitializes base class values when it is used as the initializer by the constructor of a derived class.
Top

  NameDescription
Public propertyDispatcherGets the Dispatcher this DispatcherObject is associated with. (Inherited from DispatcherObject.)
Protected propertyItemGets or sets the data being stored for the specified source.
Protected propertyReadLockEstablishes a read-lock on the underlying data table, and returns an IDisposable.
Protected propertyWriteLockEstablishes a write-lock on the underlying data table, and returns anIDisposable.
Top

  NameDescription
Public methodCheckAccessDetermines whether the calling thread has access to this DispatcherObject. (Inherited from DispatcherObject.)
Protected methodDeliverEventDelivers the event being managed to each listener.
Protected methodDeliverEventToListDelivers the event being managed to each listener in the provided list.
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Protected methodStatic memberGetCurrentManagerReturns the WeakEventManager implementation that is used for the provided type.
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Protected methodNewListenerListReturns a new object to contain listeners to an event.
Protected methodProtectedAddHandlerAdds the specified delegate as an event handler of the specified source.
Protected methodProtectedAddListenerAdds the provided listener to the provided source for the event being managed.
Protected methodProtectedRemoveHandlerRemoves the previously added handler from the specified source.
Protected methodProtectedRemoveListenerRemoves a previously added listener from the provided source.
Protected methodPurgeRemoves inactive listener entries from the data list for the provided source. Returns true if some entries were actually removed from the list.
Protected methodRemoveRemoves all listeners for the specified source.
Protected methodScheduleCleanupRequests that a purge of unused entries in the underlying listener list be performed on a lower priority thread.
Protected methodStatic memberSetCurrentManagerSets the current manager for the specified manager type.
Protected methodStartListeningWhen 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 methodStopListeningWhen overridden in a derived class, stops listening on the provided source for the event being managed.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Public methodVerifyAccessEnforces that the calling thread has access to this DispatcherObject. (Inherited from DispatcherObject.)
Top

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 data bindings.

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

Notes to Inheritors

For an example of a custom WeakEventManager, see Weak Event Patterns.

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft