关闭应用程序是否会停止所有活动的BackgroundWorker?
简单的问题,重复一下标题:
关闭 WinForms 应用程序是否会停止所有活动的后台工作人员?
Simple question, to repeat the title:
Does closing the WinForms application stops all active BackgroundWorkers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
是的,确实如此。
BackgroundWorker.RunWorkerAsync
只需在内部委托上调用BeginInvoke
,该委托又将请求排队到ThreadPool
。由于所有 ThreadPool 线程都是 背景,是的,当应用程序结束时它也会结束。但是,请记住:
通过“关闭 WinForms 应用程序”,我假设关闭主
Form
实例(通常是传递给Application 的实例) .Run
在 Visual Studio 自动生成的Program
类中)。如果您有一个带有后台工作程序的子窗口,它不会自动停止其BackgroundWorker
。让后台线程在应用程序退出时中止不是推荐的结束线程的方法,因为您无法保证它将在哪里中止。更好的方法是在关闭之前向工作线程发出信号,等待它正常完成,然后退出。
详细信息:Delegate.BeginInvoke、MSDN 关于线程池,Thread.IsBackground
Yes, it does.
BackgroundWorker.RunWorkerAsync
simply callsBeginInvoke
on a internal delegate, which in turn queues the request to theThreadPool
. Since allThreadPool
threads are background, yes, it will end when the application ends.However, keep in mind that:
By "closing the WinForms application" I am presuming closing the main
Form
instance (that's generally the one passed toApplication.Run
in theProgram
class autogenerated by Visual Studio). If you have a child window with a background worker, it will not stop itsBackgroundWorker
automatically.Letting a background thread be aborted on application exit is not the recommended way to end the thread, as you have no guarantees where it will be aborted. A much better way would be to signal the worker before closing, wait for it to finish gracefully, and then exit.
More info: Delegate.BeginInvoke, MSDN on Thread Pooling, Thread.IsBackground
在主 (UI) 线程停止后,线程可以继续执行的唯一方法是通过创建新的 Thread 实例并将 IsBackground 设置为 false 来显式创建该线程。如果你不这样做(或者如果你使用产生后台线程的ThreadPool - 或者内部也使用ThreadPool的BackgroundWorker),你的线程将是后台线程,并且将在主线程结束时终止。
The only way a thread can go on executing after your main (UI) thread has stopped is if it has been created explicitely, by creating a new Thread instance and setting the IsBackground to false. If you don't (or if you use the ThreadPool which spawns background threads - or the BackgroundWorker which also uses the ThreadPool internally) your thread will be a background thread and will be terminated when the main thread ends.
BackgroundWorker 线程是后台线程(ThreadPool 线程),< a href="http://msdn.microsoft.com/en-us/library/h339syd0.aspx" rel="noreferrer">当应用程序终止时它也会终止。
BackgroundWorker threads are background threads (ThreadPool threads), which die when the application dies.
是的,会的。我编写了这个简单的表单,关闭表单会退出应用程序:
Yes, it will. I wrote this simple form, and closing the form exits the application:
如果应用程序完全关闭(因为没有什么可以阻止它关闭),您的后台工作人员也将消失。
If the application completely closes (as in nothing is preventing it from shutting down) your background workers will also be gone.
一旦进程消失,所有关联的线程也随之消失。
Once the process is gone all associated threads are gone as well.
首先,为了让这个答案变得简单:
当一个进程关闭时,它的所有线程都已终止。这是毫无疑问的。
按照我的解释,这个问题就变成了:
这个问题的答案是:不,他们不会。
First of all, just to make this answer simple:
When a process has closed, all of its threads have terminated. There's no question about this.
The question, as I interpret it, thus becomes:
The answer to that question is: No, they won't.
我想是的。因为线程与进程相关联,如果进程关闭,所有线程都必须结束。
I think yes. Because threads are associated with process and if the process is closed all threads has to end.