What is difference between a background and foreground thread ?
By default, the threads are foreground threads. So when we create a thread the default value of IsBackground property would be false. Background Thread. Background threads are threads which will get terminated when all foreground threads are closed. The application won't wait for them to be completed.
In programming, a background thread is a thread that runs behind the scenes, while the foreground thread continues to run. For instance, a background thread may perform calculations on user input while the user is entering information using a foreground thread.
We can do that by using callbacks. In this example, the callback is executed in the calling thread, which is a background thread. This means that you cannot modify or communicate directly with the UI layer until you switch back to the main thread.
Foreground threads continue to run until the last foreground thread is terminated. When all the foreground threads are stopped, the application is closed. The default threads that are created are foreground threads.
See this page:
Foreground threads have the ability to prevent the current application from terminating. The CLR will not shut down an application (which is to say, unload the hosting AppDomain) until all foreground threads have ended.
Background threads (sometimes called daemon threads) are viewed by the CLR as expendable paths of execution that can be ignored at any point in time (even if they are currently laboring over some unit of work). Thus, if all foreground threads have terminated, any and all background threads are automatically killed when the application domain unloads.
From MSDN:
Background threads are identical to foreground threads with one exception: a background thread does not keep the managed execution environment running.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With