栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用匿名委托进行事件处理时的垃圾回收

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用匿名委托进行事件处理时的垃圾回收

我知道这个问题是古老的,但是地狱-我找到了,我认为其他人也可能如此。我正在尝试解决一个相关问题,可能会有一些见识。

您提到了达斯汀·坎贝尔(Dustin Campbell)的WeakEventHandler-
实际上,它确实无法与匿名方法一起使用。当我意识到a)在99%的情况下我需要这样的东西时,我正在尝试摆弄一些东西,他最初的解决方案会更安全,并且b)在少数情况下我必须(注意:拥有)而不是“因为lambda太漂亮和简洁了”),如果您有点聪明,就有可能使它起作用。

您的示例似乎完全是一次性的情况,其中有些棘手的问题可能会导致一个相当简洁的解决方案。

public static class linker {    public static void link(Publisher publisher, Control subscriber) {        // anonymous method references the subscriber only through weak         // references,so its existance doesn't interfere with garbage collection        var subscriber_weak_ref = new WeakReference(subscriber);        // this instance variable will stay in memory as long as the  anonymous        // method holds a reference to it we declare and initialize  it to         // reserve the memory (also,  compiler complains about uninitialized        // variable otherwise)        EventHandler<ValueEventArgs<bool>> handler = null;        // when the handler is created it will grab references to the  local         // variables used within, keeping them in memory after the function         // scope ends        handler = delegate(object sender, ValueEventArgs<bool> e) { var subscriber_strong_ref = subscriber_weak_ref.Target as Control; if (subscriber_strong_ref != null)      subscriber_strong_ref.Enabled = e.Value; else {     // unsubscribing the delegate from within itself is risky, but     // because only one instance exists and nobody else has a     // reference to it we can do this     ((Publisher)sender).EnabledChanged -= handler;     // by assigning the original instance variable pointer to null     // we make sure that nothing else references the anonymous     // method and it can be collected. After this, the weak     //  reference and the handler pointer itselfwill be eligible for     // collection as well.     handler = null;  }        };        publisher.EnabledChanged += handler;    }}

据传WPF弱事件模式会带来很多开销,因此在这种特殊情况下,我将不使用它。此外,在WinForm应用程序中引用核心WPF库似乎也很繁重。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/568437.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号