weak-events

C# language: why WeakReference or Weak Event Pattern?

牧云@^-^@ 提交于 2020-01-03 01:55:12
问题 I'm reading "The C# Language", 4th edition, it talks about WeakReference and Weak Event Pattern : CHRISTIAN NAGEL: Memory leaks often result from wrong usage of events. If client objects attach to events but do not detach from them, and the reference to the client object is no longer used, the client object still cannot be reclaimed by the garbage collector because the reference by the publisher remains. This can be avoided by (1) detaching of events when the client object is no longer used,

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

眉间皱痕 提交于 2019-12-17 15:45:10
问题 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

GC and weak events

回眸只為那壹抹淺笑 提交于 2019-12-12 03:39:38
问题 I am surprised not being able to find anything on the subject. Garbage collection is not deterministic (it will occur some times later ). Does that mean what weak event handlers may (will?) continue being called for object which is not referenced? This could be a reason of problems (performance, disposed object state, etc.), right? If I must use weak events, is it a good idea (is it enough?) to call GC.Collect() explicitly to avoid such problems? P.S.: I have performance problem with some

Weak events and GC

社会主义新天地 提交于 2019-12-10 22:33:28
问题 I am using weak events when I can't deterministically unsubscribe (otherwise I would prefer += and -= instead of weak event): class SomeType { public SomeType(...) { // object doesn't know when it will be removed WeakEventManager(SomeSource, EventArgs).AddHandler(someSourceInstance, nameof(SomeSource.SomeEvent), (s, e) => { ... }); } } This way if object is garbage collected, then event handler will not be called. Perfect. However. If object is not yet garbage collected (but there are no more

Why WeakEventManager does not fire an event when the sender is not the nominal?

匆匆过客 提交于 2019-12-10 16:58:53
问题 I don't like off-the-standard pattern, but I was making a quick test on my app, and I bumped against this strange behavior. Consider a normal class exposing an event, here the very common PropertyChanged, but I think could be any other. The subscriber chooses to subscribe the event via the WeakEventManager helper. Now, the "odd" thing is the actual sender reference: as long the instance is the same as was used on the subscription, everything goes fine. However, when you use another object, no

C# language: why WeakReference or Weak Event Pattern?

烂漫一生 提交于 2019-12-07 08:32:25
I'm reading "The C# Language", 4th edition, it talks about WeakReference and Weak Event Pattern : CHRISTIAN NAGEL: Memory leaks often result from wrong usage of events. If client objects attach to events but do not detach from them, and the reference to the client object is no longer used, the client object still cannot be reclaimed by the garbage collector because the reference by the publisher remains. This can be avoided by (1) detaching of events when the client object is no longer used, (2) a custom implementation of the add and remove accessors using the WeakReference class holding the

Do WPF controls use weak events in their bindings?

一曲冷凌霜 提交于 2019-12-04 09:09:56
问题 When I use databinding in WPF, my target controls are listening for events on the binding source. For example, I may have a ListView listening for CollectionChanged events on a ObservableCollection. If the lifetime of an event source is expected to exceed the lifetime of an event listener, there is a potential memory leak, and the weak event pattern should be used. Does WPF databinding follow the weak event pattern? If my ObservableCollection lives longer than my ListView , will my ListView

Example implementation of weak events using .NET's WeakEventManager

流过昼夜 提交于 2019-12-03 08:22:07
问题 Is there an example implementation of weak events using .NET's WeakEventManager? I'm trying to implement it by following the "Notes to Inheritors" in the documentation, but it is vague. For example, I can't figure out how to call ProtectedAddListener from my static AddListener function in my custom manager. 回答1: I figured it out on my own following the guidelines in the "Notes for Inheritors" section of the WeakEventManager documentation. Here's a basic implementation of WeakEventManager .

Do WPF controls use weak events in their bindings?

你。 提交于 2019-12-03 03:10:29
When I use databinding in WPF, my target controls are listening for events on the binding source. For example, I may have a ListView listening for CollectionChanged events on a ObservableCollection . If the lifetime of an event source is expected to exceed the lifetime of an event listener, there is a potential memory leak, and the weak event pattern should be used. Does WPF databinding follow the weak event pattern? If my ObservableCollection lives longer than my ListView , will my ListView be garbage collected? Here is why I suspect that WPF controls do not implement the weak event pattern.

Example implementation of weak events using .NET's WeakEventManager

橙三吉。 提交于 2019-12-02 22:06:26
Is there an example implementation of weak events using .NET's WeakEventManager ? I'm trying to implement it by following the "Notes to Inheritors" in the documentation, but it is vague. For example, I can't figure out how to call ProtectedAddListener from my static AddListener function in my custom manager. M. Dudley I figured it out on my own following the guidelines in the "Notes for Inheritors" section of the WeakEventManager documentation. Here's a basic implementation of WeakEventManager . The class sourcing the event is named PropertyValue and the event is named Changed . public class