C#中的BackgroundWorker和资源管理网
假设我在表单中创建一个后台工作程序作为组件。并启动它。 现在,如果我关闭表单,BackgroundWorker 将仍在运行。 您能否解释一下,尽管表单已关闭,并且在表单对象中创建的所有资源都已关闭,但 BackgroundWorker 仍在运行。这背后的原因是什么?这是因为它在不同的线程上运行吗? 当它的资源将由CLR重新获得时。
Let suppose I create a background worker in a form as a component.And start it.
Now if I close the form then BackgroundWorker will be still running.
Will you explain that however form has been closed and all resources created within the form object have been closed but BackgroundWorker is still running. What is reason behind this?. Is this because of it is runnibg on a different thread.
And when it resources will be regained by CLR.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
默认情况下,后台工作人员不会连接到其创建所在的表单或线程。
后台工作人员是一个与任何其他对象一样的对象。当不再有对它的活跃引用时,它将被收集。
因此,这实际上取决于对象的创建方式和位置,而且主要取决于谁仍然拥有对它的引用。
人们往往忘记的是,这些事件也是参考。因此,如果某个地方有另一个对象正在侦听工作人员的事件,则工作人员仍将被引用,因此不会被收集。
注意:
Form.Close() 将对话框从视线中移除,并调用 Closing() 和 Closed() 方法。您仍然可以访问该表格并稍后将其带回。
Form.Dispose() 销毁对话框并将其资源释放回操作系统。它不调用表单的 Closing() 和 Closed() 方法。一旦处理完毕,您可能不记得表格了。 Dispose() 还将调用所有 Form 组件的 Dispose() 方法。
The background worker isn't by default connected to the form or thread it's created in.
A background worker is an object like any other object. It will get collected when there are no more active references to it.
So it really depends on how and where the object was created, and mostly - who is still has references to it.
What people tend to forget is that the events are also references. So if there's another object somewhere that is listening to the worker's events, the worker will still be referenced and so it won't be collected.
Note:
Form.Close() removes the dialog from sight and calls the Closing() and Closed() methods. You can still access the form and bring it back later on.
Form.Dispose() destroys the dialog and frees its resources back to the operating system. It does not call the form’s Closing() and Closed() methods. Once disposed, you may not recall a form. The Dispose() will also call the Dispose() method of all the Form's components.
后台工作人员将随表单一起进行处理,除非您故意抑制处理。如果是这种情况,那么应该在不确定的时间进行垃圾收集。我说应该是因为事情可能会引用后台工作人员。至于线程,我相信它将在处理中被清理,应该在关闭时从表单中调用。
如果窗体是主窗体,并且在关闭时导致主前台线程关闭,则所有后台线程也将关闭/死亡。
The background worker will be disposed with the form, unless you deliberately suppress dispose. If this is the case then it should be garbage collected at an indeterminate time. I say should because things could potentially hold reference to the background worker. As for the thread, I believe it will be cleaned in the dispose, which should be called from the form when it closes.
If the form is the main form, and on closing causes the main foreground thread to close, then all background threads will close / die also.
当您关闭表单并释放它时,BackgroundWorker 也会释放它,但它不会停止线程的运行。所以只有在DoWork结束后才会被彻底销毁。
您可以通过取消表单的关闭并在后台工作线程上调用
CancelAsync
以表明您希望其完成来解决此问题。等到它完成后,您才应该真正关闭表单。When you close your form and dispose it, BackgroundWorker will dispose too, but it does not stop the thread running. So it will be completely destroyed only after the DoWork finishes.
You can solve this by cancelling the closing of the Form and calling
CancelAsync
on the background worker to signal that you want it to finish. Wait until it finishes and only after that you should actually close the form.