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
2
votes
0 answers

Can anyone explain me why there's no WeakDelegate in .NET?

I see no architectural constraints for implementing this entity. .NET 4.5 does not have it, and WeakEventManager is still in the game. I don't think guys in MS are lazy or stupid, so I suspect myself missing something very important. But what?...
Pavel Voronin
  • 13,503
  • 7
  • 71
  • 137
1
vote
2 answers

C# language: why WeakReference or Weak Event Pattern?

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…
athos
  • 6,120
  • 5
  • 51
  • 95
1
vote
1 answer

Microsoft.Win32.SystemEvents events don't work with WeakEventManager

When I do WeakEventManager .AddHandler(null, nameof(SystemEvents.DisplaySettingsChanged), OnDisplaySettingsChanged); My OnDisplaySettingsChanged never gets called. However, if I instead use normal event subscribtion via…
torvin
  • 6,515
  • 1
  • 37
  • 52
1
vote
1 answer

C# listening to 3rd party long living event using weak reference

I am attaching to a 3rd party, long living Deleted event publisher, which ends up keeping my possibly short lived objects alive due to the event handler. The Deleted event is most likely never triggered, I just have to handle things if it is. It's…
atlemann
  • 173
  • 1
  • 7
1
vote
1 answer

emddudley's WeakEventManager's sample

I am trying to adapt Michael Dudley's sample code for my own implementation of a weak event manager: Example implementation of weak events using .NET's WeakEventManager I was wondering where would I put my user code for my own handler? I see…
Kevin
  • 11
  • 3
0
votes
0 answers

How to raise weak events?

I'm reading the MS docs on how to implement the weak event pattern. I've tried the first two (since I'm looking for the simplest way) but I can't find a way to raise the event. The existing weak event managers like the PropertyChangedEventManager,…
Hossein Ebrahimi
  • 632
  • 10
  • 20
0
votes
0 answers

Is there any safe non-leaky pattern for an abandonable user of an INotifyPropertyChanged?

Is there any pattern by which a consumer of an INotifyPropertyChanged can avoid memory leaks even in circumstances where an arbitrary number of instances of the consumer may be created during the lifetime of a particular INotifyPropertyChanged…
supercat
  • 77,689
  • 9
  • 166
  • 211
0
votes
0 answers

PropertyChangedEventManager will call a function but NOT a lambda. Why?

In my C# app (.NET 6) One of my class' constructors hooks up to a PropertyChanged notification of one of the arguments passed in. I did it like this: public ProfileSource(ISystemService sys, Routine routine, Aspect aspect) : base(sys) { …
Joe
  • 5,394
  • 3
  • 23
  • 54
0
votes
0 answers

C# Use Weak Event For FileSystemWatcher.Created Event

I am trying to make a program that watches a folder for file creation events and does some processing on the files. My current implementation works using: FileSystemWatcher fileSystemWatcher = new FileSystemWatcher(); fileSystemWatcher.Created +=…
0
votes
0 answers

Is the Weak Event Pattern weak for both source and listener?

Reading about the Weak Event Patterns it is clear to me that it allows for garbage collecting the listener when it is not longer referenced but is the reciprocal true also ? Meaning does this pattern allows for garbage collecting the source when it…
CitizenInsane
  • 4,755
  • 1
  • 25
  • 56
0
votes
1 answer

Is it possible to implement a WeakEventManager for UIElement.LayoutUpdated?

I have an application that consumes LayoutUpdated-events and need to register them weak. Here is the problem, I got stuck on, during implementation of the WeakEventManager internal class WeakLayoutUpdatedManager : WeakEventManager { [..] …
m e
  • 71
  • 4
0
votes
0 answers

WPF weakly subscribe to the IsEnabledChanged event

Is it possible to weakly subscribe to the UIElement.IsEnabledChanged event? Neither the WeakEventManager WeakEventManager .AddHandler(uie,…
wondra
  • 3,271
  • 3
  • 29
  • 48
0
votes
1 answer

WeakEventManager - event handler is not called

I am not able to reproduce the issue (and project is too big to post it here, plus I am not sure what are related parts to post) and I need ideas of what could go wrong here. I have abstract class with static event public abstract partial class A :…
Sinatr
  • 20,892
  • 15
  • 90
  • 319
0
votes
0 answers

GC and weak events

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?…
Sinatr
  • 20,892
  • 15
  • 90
  • 319
0
votes
0 answers

The right way to implement Weak Event Pattern for shared property MVVM

I'm preparing an application that will have multiple objects, each one will have an integer id and string data. I want the data of the objects with the same id to be the same. I will call this object SharedObject (this object acts like a ViewModel…