手柄与的AddHandler [英] Handles vs. AddHandler
问题描述
有没有优势,动态地安装/拆除事件处理程序?
Is there an advantage to dynamically attaching/detaching event handlers?
会手动拆卸处理有助于确保没有剩余释放的对象的参考?
Would manually detaching handlers help ensure that there isn't a reference remaining to a disposed object?
推荐答案
这是不使用的AddHandler与手柄的问题。
It's not a question of using AddHandler versus Handles.
如果你所关心的关于你的事件处理程序与垃圾收集干扰,你应该使用RemoveHandler,无论处理器是如何连接的。在窗体或控件的Dispose方法,删除任何处理程序。
If you are concerned about the reference to your event handler interfering with garbage collection, you should use RemoveHandler, regardless of how the handler was attached. In the form or control's Dispose method, remove any handlers.
我有情况在Windows窗体应用程序(NET 1.1天),其中一个事件处理程序将被要求控制,没有其他对它们的引用(和它的所有意图和目的都死了,我还以为是GC ED) - 非常难于调试
I have had situations in Windows Forms apps (.NET 1.1 days) where an event handler would be called on controls that had no other references to them (and which for all intents and purposes were dead and I would have thought been GC'ed) -- extremely hard to debug.
我会用RemoveHandler摆脱对控制处理程序,你是不是打算重用。
I would use RemoveHandler to get rid of handlers on controls that you are not going to reuse.
这篇关于手柄与的AddHandler的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!