BackgroundWorker 事件处理程序
我有一个 BackgroundWorker
对象,我实例化该对象以在后台线程上异步执行数据库进程。我有 DoWor
k 和 RunWorkerCompleted
的事件处理程序。
我可以看出 BackgroundWorker
正在自行处置,因为我在 Dispose
事件处理程序中添加了一个 MessageBox
。
我的问题是:
是否有必要分离事件处理程序以确保内存被清理并且不存在内存泄漏?
I have a BackgroundWorker
object that I instantiated to perform a DB process on a background thread ansynchronously. I have event handlers for DoWor
k and RunWorkerCompleted
.
I can tell that the BackgroundWorker
is disposing of itself because I added a MessageBox
into the Disposed
event handler.
My question is this:
Is it necessary to detach the event handlers to ensure that the memory is cleaned up and that there are not memory leaks?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果事件发布者正在被垃圾收集,则无需取消订阅。仅当事件订阅者(处理程序委托的目标)需要在发布者之前获得垃圾回收资格时,您才需要取消订阅事件。
If an event publisher is being garbage collected, then there's no need to unsubscribe. You only need to unsubscribe from events if the event subscriber (the target of the handler delegate) needs to become eligible for garbage collection before the publisher does.