如何取消订阅使用 lambda 表达式的事件?

发布于 2024-07-19 02:05:43 字数 212 浏览 2 评论 0原文

我有以下代码让 GUI 响应集合中的更改。

myObservableCollection.CollectionChanged += ((sender, e) => UpdateMyUI());

首先,这是一个好方法吗?

第二:取消订阅此活动的代码是什么? 是否相同,但带有 -= (然后又是完整的匿名方法)?

I have the following code to let the GUI respond to a change in the collection.

myObservableCollection.CollectionChanged += ((sender, e) => UpdateMyUI());

First of all is this a good way to do this?

Second: what's the code to unsubscribe from this event? Is it the same but with -= (and then the complete anonymous method again)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风筝有风,海豚有海 2024-07-26 02:05:43

首先...是的,这是一种很好的方法,它干净、形式小且易于阅读和使用。 理解...当然需要注意的是“除非您以后想取消订阅”。

我相信 Jon Skeet 之前指出过
“当涉及到使用匿名方法创建的委托的等效性时,规范明确不保证任何一种行为。”

因此,如果您稍后需要取消订阅该事件,您最好实际创建一个委托实例,以便稍后可以保留该引用。

var myDelegate = delegate(sender, e){UpdateMyUI()};

myObservableCollection.CollectionChanged += myDelegate;

myObservableCollection.CollectionChanged -= myDelegate;

First of all... yes its a good way of doing it, it's clean, small form and easy to read & understand... the caveat of course is "Unless you later want to unsubscribe".

I believe Jon Skeet pointed out before that
"the specification explicitly doesn't guarantee the behaviour either way when it comes to equivalence of delegates created with anonymous methods."

So if you need to unsubscribe from the event at a later time, you'd be best to actually create a delegate instance so you can hang onto the reference for later.

var myDelegate = delegate(sender, e){UpdateMyUI()};

myObservableCollection.CollectionChanged += myDelegate;

myObservableCollection.CollectionChanged -= myDelegate;
时光沙漏 2024-07-26 02:05:43

如果您需要取消订阅事件,则需要实例化引用。 不幸的是,这意味着您不能使用该特定语法。

If you need to unsubscribe from an event, you need an instanced reference. Unfortunately, that means you can't use that particular syntax.

月棠 2024-07-26 02:05:43

这是一个不错的方法,除非 myObservableCollection 的寿命比“this”长,在这种情况下,您可能最终会出现内存泄漏,因为在幕后创建的委托将保留对“this”的引用,这将使它保持活力。 如果您重复创建和“销毁”正在侦听事件的任何内容,您会发现它们没有被垃圾收集器收集。

如果这是一个问题,您可以按照答案中建议的路线进行操作,保留对处理程序的引用,您必须首先创建该处理程序。

另一个解决方案是使用弱引用创建一个事件处理程序,如果没有其他引用,该事件处理程序将允许收集订阅者。 我在这个问题和答案中探索了这个解决方案< /a>.

It's an ok way to go, unless myObservableCollection is going to live longer than 'this', in which case you could end up with a memory leak, as the delegate which is created behind the scenes will conserve a reference to your 'this', which will keep it alive. If you are repeatedly creating and 'destroying' whatever is listening to the event, you will find that they aren't being collected by the garbage collector.

If this is a problem, you can go the route suggested in the answers, conserving a reference to the handler, which you must create first.

Another solution is to use weak references to create an event handler which will allow the subscriber to be collected if there are not other references. I've explored this solution in this question and answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文