Click or drag to resize

WeakEventHandler Class

Mediates an EventHandler event, holding only a weak reference to the event sink, and a weak reference to the event source for removing the event handler. Thus there is no reference created from the event source to the event sink, and both the event sink and the event source can be garbage collected.
Inheritance Hierarchy
SystemObject
  AltaxoWeakEventHandler

Namespace: Altaxo
Assembly: AltaxoCore (in AltaxoCore.dll) Version: 4.8.3179.0 (4.8.3179.0)
Syntax
C#
public class WeakEventHandler

The WeakEventHandler type exposes the following members.

Constructors
 NameDescription
Public methodWeakEventHandler(EventHandler, Object, String) Initializes a new instance of the WeakEventHandler class.
Public methodWeakEventHandler(EventHandler, Type, String) Initializes a new instance of the WeakActionHandler class for a static event.
Top
Properties
 NameDescription
Public propertyEventSourceGets the event source. Attention! Use the returned value only locally, otherwise, you will get a strong reference that you wanted to avoid.
Top
Methods
 NameDescription
Public methodEqualsDetermines whether the specified object is equal to the current object.
(Inherited from Object)
Public methodEventSink Handles the event from the original source. You must not call this method directly. However, it can be neccessary to use the method reference if the implicit casting fails. See remarks in the description of this class.
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object)
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)
Public methodRemoveRemoves the event handler from the event source, using the stored remove action..
Public methodToStringReturns a string that represents the current object.
(Inherited from Object)
Top
Operators
 NameDescription
Public operatorStatic member(WeakEventHandler to EventHandler) Converts this instance to an EventHandler that can be used to add or remove it from/to an event.
Top
Remarks
Typical use:
C#
source.Changed += new WeakEventHandler(this.EhHandleChange, source, nameof(source.Changed));
Sometimes it might be neccessary to explicitly use the event handler method of this instance:
C#
source.Changed += new WeakEventHandler(this.EhHandleChange, source, nameof(source.Changed)).EventSink;
You can even maintain a reference to the WeakActionHandler instance in your event sink instance, in case you have to remove the event handling programmatically:
C#
_weakEventHandler = new WeakEventHandler(this.EhHandleChange, source, nameof(source.Changed)); // weakEventHandler is an instance variable of this class
source.Changed += _weakEventHandler;
.
.
.
source.Changed -= _weakEventHandler;
See Also