<tfoot id='XfHK6'></tfoot>
<legend id='XfHK6'><style id='XfHK6'><dir id='XfHK6'><q id='XfHK6'></q></dir></style></legend>

    • <bdo id='XfHK6'></bdo><ul id='XfHK6'></ul>
  • <i id='XfHK6'><tr id='XfHK6'><dt id='XfHK6'><q id='XfHK6'><span id='XfHK6'><b id='XfHK6'><form id='XfHK6'><ins id='XfHK6'></ins><ul id='XfHK6'></ul><sub id='XfHK6'></sub></form><legend id='XfHK6'></legend><bdo id='XfHK6'><pre id='XfHK6'><center id='XfHK6'></center></pre></bdo></b><th id='XfHK6'></th></span></q></dt></tr></i><div id='XfHK6'><tfoot id='XfHK6'></tfoot><dl id='XfHK6'><fieldset id='XfHK6'></fieldset></dl></div>

    <small id='XfHK6'></small><noframes id='XfHK6'>

        我是否应该始终断开 Dispose 方法中的事件处理程序?

        时间:2023-09-15

            <tbody id='j9yTy'></tbody>
            <i id='j9yTy'><tr id='j9yTy'><dt id='j9yTy'><q id='j9yTy'><span id='j9yTy'><b id='j9yTy'><form id='j9yTy'><ins id='j9yTy'></ins><ul id='j9yTy'></ul><sub id='j9yTy'></sub></form><legend id='j9yTy'></legend><bdo id='j9yTy'><pre id='j9yTy'><center id='j9yTy'></center></pre></bdo></b><th id='j9yTy'></th></span></q></dt></tr></i><div id='j9yTy'><tfoot id='j9yTy'></tfoot><dl id='j9yTy'><fieldset id='j9yTy'></fieldset></dl></div>
            <legend id='j9yTy'><style id='j9yTy'><dir id='j9yTy'><q id='j9yTy'></q></dir></style></legend>
            • <bdo id='j9yTy'></bdo><ul id='j9yTy'></ul>
              • <tfoot id='j9yTy'></tfoot>

                  <small id='j9yTy'></small><noframes id='j9yTy'>

                  本文介绍了我是否应该始终断开 Dispose 方法中的事件处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我使用 C# 工作,我的工作场所有一些代码标准.其中之一是我们连接的每个事件处理程序(例如 KeyDown)必须在 Dispose 方法中断开连接.有什么好的理由吗?

                  I'm working in C# and my workplace has some code standards. One of them is that each event handler we connect (such as KeyDown) must be disconnected in the Dispose method. Is there any good reason for that?

                  推荐答案

                  除非您希望事件的发布者比订阅者的寿命长,否则没有理由删除事件处理程序,不.

                  Unless you expect the publisher of the event to outlive the subscriber, there's no reason to remove the event handler, no.

                  这是民间传说发展起来的主题之一.你真的只需要用正常的术语来考虑它:发布者(例如按钮)有对订阅者的引用.如果发布者和订阅者都可以同时进行垃圾回收(这很常见),或者如果发布者更早可以进行垃圾回收,则不存在 GC 问题.

                  This is one of those topics where folk lore has grown up. You really just need to think about it in normal terms: the publisher (e.g. the button) has a reference to the subscriber. If both the publisher and the subscriber will be eligible for garbage collection at the same time anyway (as is common) or if the publisher will be eligible for garbage collection earlier, then there's no GC issue.

                  静态事件会导致 GC 问题,因为它们实际上是无限长寿命的发布者 - 我会尽可能完全阻止静态事件.(我很少发现它们有用.)

                  Static events cause a GC problem because they're effectively an infinitely-long-lived publisher - I would discourage static events entirely, where possible. (I very rarely find them useful.)

                  另一个可能的问题是,如果您明确想要停止侦听事件,因为如果引发事件,您的对象将出现异常行为(例如,它将尝试写入已关闭的流).在这种情况下,是的,您应该删除处理程序.这很可能是在您的类已经实现 IDisposable 的情况下.这将是不寻常的 - 尽管并非不可能 - 值得实现 IDisposable just 以删除事件处理程序.

                  The other possible issue is if you explicitly want to stop listening for events because your object will misbehave if the event is raised (e.g. it will try to write to a closed stream). In that case, yes, you should remove the handler. That's most likely to be in the case where your class implements IDisposable already. It would be unusual - though not impossible - for it to be worth implementing IDisposable just to remove event handlers.

                  这篇关于我是否应该始终断开 Dispose 方法中的事件处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在通用 Windows 平台中更改按钮样式 下一篇:文本框的输入按键触发事件

                  相关文章

                      <bdo id='cPQEY'></bdo><ul id='cPQEY'></ul>

                    <small id='cPQEY'></small><noframes id='cPQEY'>

                    <tfoot id='cPQEY'></tfoot>

                      <i id='cPQEY'><tr id='cPQEY'><dt id='cPQEY'><q id='cPQEY'><span id='cPQEY'><b id='cPQEY'><form id='cPQEY'><ins id='cPQEY'></ins><ul id='cPQEY'></ul><sub id='cPQEY'></sub></form><legend id='cPQEY'></legend><bdo id='cPQEY'><pre id='cPQEY'><center id='cPQEY'></center></pre></bdo></b><th id='cPQEY'></th></span></q></dt></tr></i><div id='cPQEY'><tfoot id='cPQEY'></tfoot><dl id='cPQEY'><fieldset id='cPQEY'></fieldset></dl></div>

                    1. <legend id='cPQEY'><style id='cPQEY'><dir id='cPQEY'><q id='cPQEY'></q></dir></style></legend>