弱参考和事件处理 [英] WeakReference and event handling

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

问题描述

通过WeakReference实现事件处理是一个很好的做法,如果该事件是唯一持有该引用的事件,并且我们需要该对象被垃圾回收?

Is it a good practice to implement event handling through WeakReference if that event is the only thing holding the reference and that we would need the object to be garbage collected?

作为这个参数:

有人说如果你订阅了,你有责任取消订阅你应该这样做。

Folks say that if you subscribe to something it’s your responsibility to unsubscribe and you should do it.

推荐答案

可以让事情取消订阅的习惯很好,但有时候这是一个明显的清理方法,可以做到这一点。我们最近在这个问题上发布了博客文章;它包括使用WeakReference可以轻松订阅事件的方法。

It is good to get in the habit of unsubscribing from events when you can, but sometimes there isn't an obvious "cleanup" method where it can be done. We recently posted a blog article on this subject; it includes methods that make it easy to subscribe to an event with a WeakReference.

这篇关于弱参考和事件处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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