I want to point out one behavior of BackgroundWorker class that wasn't mentioned yet. You can make a normal Thread to run in background by setting the Thread.IsBackground property.
Background threads are identical to foreground threads, except that background threads do not prevent a process from terminating. [1]
You can test this behavoir by calling the following method in the constructor of your form window.
void TestBackgroundThread()
{
var thread = new Thread((ThreadStart)delegate()
{
long count = 0;
while (true)
{
count++;
Debug.WriteLine("Thread loop count: " + count);
}
});
// Choose one option:
thread.IsBackground = true; // <--- This will make the thread run in background
thread.IsBackground = false; // <--- This will delay program termination
thread.Start();
}
When the IsBackground property is set to true and you close the window, then your application will terminate normaly.
But when the IsBackground property is set to false (by default) and you close the window, then just the window will disapear but the process will still keep running.
The BackgroundWorker class utilize a Thread that runs in the background.