是否有必要从事件退订? [英] is it necessary to unsubscribe from events?

查看:114
本文介绍了是否有必要从事件退订?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何严重的可能是为NOT在C#中的事件退订?
它是强制性的或最好的行动?

How serious it might be to NOT unsubscribe from the events in c#? Is it mandatory or advisable action?

推荐答案

这是从的 MSDN文档,你应该考虑到

This is the important part from the MSDN documentation that you should take into consideration

要prevent从事件处理程序
  当事件是被调用
  提出,只需从退订
  事件。为了prevent资源
  泄漏,它退订是重要
  从事件发生之前,你处理的
  用户对象。直到你
  从事件退订时,
  构成了这一多路广播委托
  事件在出版对象具有
  参考的委托,
  封装了用户的事​​件
  处理程序。 只要出版
  对象认为参考,您的
  用户对象不会被垃圾
  收集。

To prevent your event handler from being invoked when the event is raised, simply unsubscribe from the event. In order to prevent resource leaks, it is important to unsubscribe from events before you dispose of a subscriber object. Until you unsubscribe from an event, the multicast delegate that underlies the event in the publishing object has a reference to the delegate that encapsulates the subscriber's event handler. As long as the publishing object holds that reference, your subscriber object will not be garbage collected.

这篇关于是否有必要从事件退订?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆