Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In CLR, what is difference between a background and foreground thread?

What is difference between a background and foreground thread ?

like image 266
Sandbox Avatar asked Aug 25 '09 19:08

Sandbox


People also ask

What is the background and foreground thread in C#?

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.

What are background threads?

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.

Which thread is executed in the background?

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.

Which thread executes in foreground by default?

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.


2 Answers

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.

like image 120
Zed Avatar answered Oct 21 '22 12:10

Zed


From MSDN:

Background threads are identical to foreground threads with one exception: a background thread does not keep the managed execution environment running.

like image 42
Matthew Jones Avatar answered Oct 21 '22 10:10

Matthew Jones