Handles vs. AddHandler

前端 未结 7 488
花落未央
花落未央 2021-01-15 18:53

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 d

7条回答
  •  情歌与酒
    2021-01-15 19:22

    It's not a question of using AddHandler versus Handles.

    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.

    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.

    I would use RemoveHandler to get rid of handlers on controls that you are not going to reuse.

提交回复
热议问题