取消订阅活动-性能受到影响吗? [英] Unsubscribing from events - performance hit?

查看:63
本文介绍了取消订阅活动-性能受到影响吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下代码(来自效果报告):

Consider the following code (from a performance report):

这是属性通知侦听器组件的一部分.方法 OnItemPropertyChanged 是具有 PropertyChangedEventHandler 签名的私有实例绑定方法.该方法被称为约100.000次,并导致应用程序的严重延迟.

This is part of a property notificiation listener component. The method OnItemPropertyChanged is a private instance-bound method with the PropertyChangedEventHandler signature. This method is called around 100.000 times and is causing significant delays in the application.

是否存在与(取消)订阅事件相关的性能考虑?是否有解释为什么会导致这种性能下降?

Are there performance considerations related to (un)subscribing events? Is there an explanation to why this would cause such a performance hit?

推荐答案

关于许多评论表明的设计缺陷:我们强烈认为用户不会动摇将带有100k个对象的对象图整体放入用户界面;这是正在进行的改进过程的一部分,有望在将来得到解决.

With regards to the design flaw that many comments suggest: we have strongly opinioned users that will not sway from putting an object graph with 100k objects as a whole into the user interface; this is part of an ongoing improvement process and will hopefully be resolved in the future.

sharedHandler Method 引用传递给取消订阅操作符之间没有显着差异.

There was no significant difference between the sharedHandler and Method reference being passed to the unsubscription operator.

使用弱事件管理器类可消除对性能的影响;可以创建或不创建 Marc Gravell 这样的代表.也许这与此类以不同的方式创建其自己的事件侦听器有关,而不是使用其自变量中提供的事件侦听器.我已经查看了,但是无法找到关于为什么这种模式似乎能快速运行的解释(因为仍然调用了 + = -= 运算符).

Using the weak event manager classes removes the performance hit; with or without creating delegates like Marc Gravell suggests. Perhaps this is related to this class creating its own event listener in a different way instead of using the one supplied in its argument. I have looked into the source but could not find an explanation as to why this pattern does seem to work fast (since the += and -= operators are still called).

这篇关于取消订阅活动-性能受到影响吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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