WithEvents/Handles better than Remove/AddHandler?

前端 未结 3 2072
轮回少年
轮回少年 2020-12-20 16:35

From a memory point of view (remove an added handler after utilization, etc.), is WithEvents and Handles usage preferable to RemoveHandler

3条回答
  •  星月不相逢
    2020-12-20 16:55

    I prefer WithEvents/Handles in situations where it is applicable, because it better expresses what the code is supposed to be doing. One caveat with "WithEvents/Handles" is that any object which receives events from a longer-lived object should implement IDisposable and should set all its WithEvents variables to Nothing when it is disposed, to ensure all events are unwired. Detaching events when using AddHandler/RemoveHandler is just as necessary, but perhaps more obvious. When using WithEvents it's somewhat easier to forget.

    BTW, I don't know of any way to automatically set all WithEvents variables to Nothing. It would seem a common enough requirement, but for whatever reason Microsoft didn't include such a feature in VB.

提交回复
热议问题