11

Simple question, to repeat the title:

Does closing the WinForms application stops all active BackgroundWorkers?

tshepang
  • 12,111
  • 21
  • 91
  • 136
Kornelije Petak
  • 9,412
  • 15
  • 68
  • 96

8 Answers8

18

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

vgru
  • 49,838
  • 16
  • 120
  • 201
7

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.

Yann Schwartz
  • 5,954
  • 24
  • 27
6

BackgroundWorker threads are background threads (ThreadPool threads), which die when the application dies.

1

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);
        }
    }
}
Philip Wallace
  • 7,905
  • 3
  • 28
  • 40
0

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

BennyM
  • 2,796
  • 16
  • 24
  • 3
    That's kind of a circular answer. If the background workers aren't stopped automatically when the app is closing, they will prevent the application from closing. – Lasse V. Karlsen Nov 06 '09 at 13:46
0

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

dkackman
  • 15,179
  • 13
  • 69
  • 123
0

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.

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
-1

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

IsmailS
  • 10,797
  • 21
  • 82
  • 134