weak-events

Is it safe to replace all standard event handler to WeakEventManager or its varian?

心不动则不痛 提交于 2019-12-01 09:10:24
Standard event handler (with operator += ) is one of the memory leakage cause (if it is not unresgistered/disposed (with -= operator)). And Microsoft solved it with WeakEventManager and its inheritance like: PropertyChangedEventManager, CollectionChangedEventManager, CurrentChangedEventManager, ErrorsChangedEventManager and so on. The simple example code with memory leakage is: public class EventCaller { public static event EventHandler MyEvent; public static void Call() { var handler = MyEvent; if (handler != null) { handler(null, EventArgs.Empty); Debug.WriteLine("============="); } } }

Is it safe to replace all standard event handler to WeakEventManager or its varian?

落花浮王杯 提交于 2019-12-01 07:42:44
问题 Standard event handler (with operator += ) is one of the memory leakage cause (if it is not unresgistered/disposed (with -= operator)). And Microsoft solved it with WeakEventManager and its inheritance like: PropertyChangedEventManager, CollectionChangedEventManager, CurrentChangedEventManager, ErrorsChangedEventManager and so on. The simple example code with memory leakage is: public class EventCaller { public static event EventHandler MyEvent; public static void Call() { var handler =

What is the “Weak Event” pattern used in WPF applications?

跟風遠走 提交于 2019-11-27 19:50:45
The WindowsBase DLL defines the IWeakEventListener event with summary: Provides event listening support for classes that expect to receive events through the WeakEvent pattern and a System.Windows.WeakEventManager. This vague description doesn't describe what the 'WeakEvent pattern' actually is. So, what is this pattern, why is it used and is it useful outside of WPF applications? EDIT Some good answers already, but no one has talked about whether this pattern is useful outside of WPF applications. It seems to me that the weak event pattern, like dependency properties, is inextricably linked