Excerpt from MSDN
A managed thread is either a background thread, or a foreground thread. The background thread does not leave the managed execution environment running, except that the background thread is the same as the foreground thread. Once all foreground threads are stopped in the managed process (where the. exe file is a managed assembly), the system stops all background threads and shuts down.
Attention |
When the runtime stops a background thread because the process is closed, an exception is not thrown in the thread. However, when a thread is stopped because the System.AppDomain.Unload (System.AppDomain) method unloads the application domain, ThreadAbortException is raised both in the background and in the foreground thread. |
Use the Thread.IsBackground property to determine whether the thread is a background thread or a foreground thread, or to change its state. You can change a thread to a background thread at any time by setting its IsBackground property to True.
Points |
The foreground or background state of the thread does not affect the results of unhandled exceptions in the thread. In the. NET Framework version 2.0, unhandled exceptions in the foreground or background thread will cause the application to terminate. See Exceptions in Managed threads. |
A thread that belongs to the managed thread pool (that is, a thread whose Isthreadpoolthread property is true) is a background thread. All threads that enter the managed execution environment from unmanaged code are marked as background threads. All the lines that are generated by creating and starting a new thread object Cheng Tume think of the foreground thread.
If you are using a thread to monitor activity, such as a socket connection, set its IsBackground property to true so that the thread does not prevent the process from terminating.
This means that once the main thread of the program is closed (the main thread code execution completes, the program shuts down), the background thread does not force the shutdown and does not throw an exception. So if there is a background thread running, make sure that the foreground thread does not shut down.
Background and foreground threads