简体   繁体   English

弱事如何运作?

[英]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'. 我目前正在学习WPF,并且偶然发现了弱事件的概念,但我真的很想“得到它”。 I have read countless articles on Stackoverflow and looked at code samples but it just isn't sinking in. 我已经在Stackoverflow上阅读了无数文章并查看了代码示例,但它并没有陷入其中。

Here's my dilemma: 这是我的困境:

  1. I understand that when an object subscribes to an event, the source of the event has to hold a reference to the subscriber. 我理解,当一个对象订阅一个事件时,事件的来源必须保持对订阅者的引用。
  2. I also understand that if the subscriber goes out of scope or is explicitly destroyed but the event source is not destroyed then the subscriber will not be garbage collected because the event source still retains a reference to the subscriber. 我也理解,如果订阅者超出范围或被明确销毁但事件源未被销毁,则订阅者将不会被垃圾收集,因为事件源仍保留对订阅者的引用。
  3. A common method of avoiding this is to explicitly un-subscribe the subscriber from the source before the object is destroyed. 避免这种情况的常见方法是在销毁对象之前明确地从源中取消订阅订阅者。 I understand that this can be a problem if the programmer is not able to determine when this will occur. 我知道如果程序员无法确定何时会发生这种情况,这可能是一个问题。

So from the above I understand how the use of events can cause memory leaks and why there is a need for a weak reference pattern but what is stopping me from understanding is how does the weak event pattern actually achieve this goal? 因此,从上面我了解事件的使用如何导致内存泄漏以及为什么需要弱参考模式但阻止我理解的是弱事件模式如何实际实现这一目标? What does it do differently? 它有什么不同的做法?

Surely even if there is a class that manages events it still has to subscribe and un-subscribe the handlers to / from the source, hence references must exist, giving the same problems with the standard way of using events. 当然,即使有一个管理事件的类,它仍然必须订阅和取消订阅源处理器的处理程序,因此引用必须存在,给出了使用事件的标准方法的相同问题。

Someone please explain to me what fundamental concept I am missing or misunderstanding and help me to 'get' the weak event pattern. 有人请向我解释一下我遗漏或误解的基本概念,并帮助我“搞”弱事件模式。

What you are missing is that Weak Events (which use Weak References under the covers, which in turn use a GCHandle ) are leveraging built-in CLR behavior for the particular case of needing to access an object without holding a strong reference to it- that is, they are not constrained by the normal "rules" that your application code is subject to. 您缺少的是弱事件(使用弱引用 ,后者又使用GCHandle )正在利用内置CLR行为来处理需要访问对象的特定情况,而不需要对其进行强引用 -是的,它们不受您的应用程序代码所遵循的正常“规则”的约束。

See http://sankarsan.wordpress.com/2008/08/09/weak-references/ http://sankarsan.wordpress.com/2008/08/09/weak-references/

Behind the scenes, the WeakEventManager is holding a weak reference to the event subscriber. 在幕后,WeakEventManager持有对事件订阅者的弱引用。 If the subscriber happens to be GC'd before the event is raised, the WeakEventManager just shrugs and says "OK, that guy's dead, I'm just going to stop trying to notify him of this event from now on" 如果订阅者在事件发生之前恰好是GC'd,那么WeakEventManager只是耸了耸肩并且说“好吧,那家伙已经死了,我只是打算从现在开始尝试通知他这个事件”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM