Questions tagged [weak-events]

Events that don't hold strong references to their subscribers.

Events that don't hold strong references to their subscribers. Therefore, an object that subscribes to a weak event is eligible for garbage collection, when there are no strong references to it.

31 questions
25
votes
2 answers

Example implementation of weak events using .NET's WeakEventManager

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…
sourcenouveau
  • 29,356
  • 35
  • 146
  • 243
18
votes
3 answers

What is the "Weak Event" pattern used in WPF applications?

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…
Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
17
votes
1 answer

How do weak events work?

I'm currently learning WPF and have stumbled upon the concept of weak events but I am really struggling to 'get it'. I have read countless articles on Stackoverflow and looked at code samples but it just isn't sinking in. Here's my dilemma: I…
Benjamin Gale
  • 12,977
  • 6
  • 62
  • 100
15
votes
2 answers

Do WPF controls use weak events in their bindings?

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…
sourcenouveau
  • 29,356
  • 35
  • 146
  • 243
6
votes
1 answer

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

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…
Mario Vernari
  • 6,649
  • 1
  • 32
  • 44
6
votes
2 answers

WeakEventManager with event name lambda expression and custom event accessors

I have been looking in to subscribing to an event using a weak event pattern. With the .NET 4.5 framework, we have a slick looking WeakEventManager class. Weakly subscribing to an event is as simple as WeakEventManager
Tallek
  • 1,575
  • 15
  • 22
5
votes
1 answer

What's a good implementation of weak events for silverlight?

I'm after a good implementation of the weak event patterns for Silverlight to avoid memory leaks. There seem to be a few implementations out there but the code is not trivial and it's hard to know which one is correct. I can't find any official…
Clement
  • 3,990
  • 4
  • 43
  • 44
5
votes
3 answers

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

Standard event handler (with operator +=) is one of the memory leakage cause (if it is not unregistered/disposed (with -= operator)). And Microsoft solved it with WeakEventManager and its inheritance like: PropertyChangedEventManager,…
Yohanes Nurcahyo
  • 601
  • 8
  • 19
4
votes
1 answer

WeakEvent garbage collection in C#

I'm thinking today at the following scenario: I have two classes A and B. A exposes an event E. B subscribes to this event "E", using a weak event handler "W". After a while nobody holds any reference to B, but the GC has not yet kicked in. While B…
Liviu Trifoi
  • 2,980
  • 1
  • 21
  • 28
4
votes
1 answer

PCL WeakEventManager from Reactive extensions disposes event in 3 - 7 minutes

I'm trying to implement WeakEventManager in PCL using the Reactive library. So the point is that it keeps a weak reference for the subscriber and each time event fires - it gets the delegate of the subscriber and fires that, but if he couldn't get…
3
votes
1 answer

Using WeakEventManager in Windows.Forms application

When using weak events as described here http://wekempf.spaces.live.com/blog/cns!D18C3EC06EA971CF!373.entry in a Windows.Forms application the WeakEventManager leaks WeakReference objects. I think this is because without a WPF message loop the…
Henrik
  • 23,186
  • 6
  • 42
  • 92
3
votes
1 answer

Why using Weak Event Pattern on controls instead of managing lifetime somewhere else?

I understand the Weak Reference and the Weak Event Pattern. One place where the weak event pattern is used is in DataBinding between Controls and DataModel. During the process of DataBinding, if the DataModel support INotifyPropertyChange, the…
Eric Ouellet
  • 10,996
  • 11
  • 84
  • 119
2
votes
1 answer

Weak events and GC

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 …
Sinatr
  • 20,892
  • 15
  • 90
  • 319
2
votes
1 answer

CollectionChangedEventManager not forwarding event for custom collection

I have a custom INotifyCollectionChanged class, which essentially just wraps around the standard ObservableCollection. Whenever something is added/removed, the CollectionChanged event is raised as expected. However, when I try to listen to this…
Steven
  • 2,437
  • 5
  • 32
  • 36
2
votes
1 answer

using WPF 4.5 Generic Weak Event Manager for Handled Routed Events?

I would like to know how to convert the subscription of "Handled" RoutedEvents to WeakEventManager? UIElement has the following method to subscribe to "Handled" RoutedEvents: UIElement.AddHandler(RoutedEvent routedEvent, Delegate handler, bool…
icube
  • 2,578
  • 1
  • 30
  • 63
1
2 3