是否关闭应用程序停止所有活动BackgroundWorkers? [英] Does closing the application stops all active BackgroundWorkers?
问题描述
简单的问题,重复标题:
Simple question, to repeat the title:
是否关闭的WinForms应用程序停止所有活动BackgroundWorkers
推荐答案
是的,是的。
BackgroundWorker.RunWorkerAsync
只需调用的BeginInvoke
在内部的委托,这反过来又请求排队到线程池
。由于所有的线程池
线程的背景,是的,这将在申请时结束结束
BackgroundWorker.RunWorkerAsync
simply calls BeginInvoke
on a internal delegate, which in turn queues the request to the ThreadPool
. Since all ThreadPool
threads are background, yes, it will end when the application ends.
不过,请记住:
-
通过关闭WinForms应用程序我假设关闭的主要
表格
实例的(这是一般人在程序传递给
类由Visual Studio自动生成)。如果你有一个后台工作的子窗口,它不会停止其Application.Run
的BackgroundWorker
自动
让一个后台线程应用程序退出中止是的不可以推荐的方式来结束线程,因为你不能保证它会被中止。更好的方法是关闭前的信号工人,等待它优雅地完成,然后退出。
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.
更多信息: Delegate.BeginInvoke ,的 MSDN上的线程池,的 Thread.IsBackground
这篇关于是否关闭应用程序停止所有活动BackgroundWorkers?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!