关闭应用程序是否会停止所有活动的BackgroundWorker?

发布于 2024-08-10 11:18:34 字数 80 浏览 8 评论 0原文

简单的问题,重复一下标题:

关闭 WinForms 应用程序是否会停止所有活动的后台工作人员?

Simple question, to repeat the title:

Does closing the WinForms application stops all active BackgroundWorkers?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

梦幻的味道 2024-08-17 11:18:34

是的,确实如此。

BackgroundWorker.RunWorkerAsync 只需在内部委托上调用 BeginInvoke,该委托又将请求排队到 ThreadPool。由于所有 ThreadPool 线程都是 背景,是的,当应用程序结束时它也会结束。

但是,请记住:

  1. 通过“关闭 WinForms 应用程序”,我假设关闭Form 实例(通常是传递给 Application 的实例) .Run 在 Visual Studio 自动生成的 Program 类中)。如果您有一个带有后台工作程序的子窗口,它不会自动停止其 BackgroundWorker

  2. 让后台线程在应用程序退出时中止不是推荐的结束线程的方法,因为您无法保证它将在哪里中止。更好的方法是在关闭之前向工作线程发出信号,等待它正常完成,然后退出。

详细信息:Delegate.BeginInvokeMSDN 关于线程池Thread.IsBackground

Yes, it does.

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.

However, keep in mind that:

  1. By "closing the WinForms application" I am presuming closing the main Form instance (that's generally the one passed to Application.Run in the Program class autogenerated by Visual Studio). If you have a child window with a background worker, it will not stop its BackgroundWorker automatically.

  2. 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

牛↙奶布丁 2024-08-17 11:18:34

在主 (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.

太阳公公是暖光 2024-08-17 11:18:34

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.

挽袖吟 2024-08-17 11:18:34

是的,会的。我编写了这个简单的表单,关闭表单会退出应用程序:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.backgroundWorker1.RunWorkerAsync();
    }

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        while (true)
        {
            Thread.Sleep(100);
        }
    }
}

Yes, it will. I wrote this simple form, and closing the form exits the application:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.backgroundWorker1.RunWorkerAsync();
    }

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        while (true)
        {
            Thread.Sleep(100);
        }
    }
}
亽野灬性zι浪 2024-08-17 11:18:34

如果应用程序完全关闭(因为没有什么可以阻止它关闭),您的后台工作人员也将消失。

If the application completely closes (as in nothing is preventing it from shutting down) your background workers will also be gone.

梦晓ヶ微光ヅ倾城 2024-08-17 11:18:34

一旦进程消失,所有关联的线程也随之消失。

Once the process is gone all associated threads are gone as well.

潜移默化 2024-08-17 11:18:34

首先,为了让这个答案变得简单:

当一个进程关闭时,它的所有线程都已终止。这是毫无疑问的。

按照我的解释,这个问题就变成了:

仍在运行的 BackgroundWorker 实例会阻止应用程序关闭吗?

这个问题的答案是:不,他们不会。

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:

Will still-running BackgroundWorker instances prevent an application from closing?

The answer to that question is: No, they won't.

故事与诗 2024-08-17 11:18:34

我想是的。因为线程与进程相关联,如果进程关闭,所有线程都必须结束。

I think yes. Because threads are associated with process and if the process is closed all threads has to end.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文