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

本秂侑毒 提交于 2020-01-01 04:17:27

问题


What is difference between a background and foreground thread ?


回答1:


From MSDN:

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




回答2:


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.




回答3:


By default, threads are foreground threads, meaning they keep the application alive for as long as any one of them is running. C# also supports background threads, which don’t keep the application alive on their own – terminating immediately once all foreground threads have ended.




回答4:


The important difference between background and foreground threads that is not mentioned yet is the following: a background thread executes only when the number of foreground threads executing is smaller than the number of processors MSDN.




回答5:


There are two types of threads -

  • Foreground Thread
  • Background Thread

    Whenever we open any application , then the main UI thread is of type Foreground thread .This is the default thread type . Suppose when we create any new thread ,By default the thread current type is foreground itself . If you want to change the type of the thread you will have to execute threadName.IsBackground = true ; Now the main story starts . What is the difference ? And why do we need these two types ?

Foreground Thread : Suppose we are creating a thread ThreadA . If we need the thread ThreadA to keep executing in spite of all other threads are aborted , even if our main UI thread is no more alive , then in this case we must keep our thread type Foreground . So if you keep your thread foreground type then even if you close your application , the foreground thread ThreadA will keep running , you can track it also in your task manager .

Background Threads : Now if you change your thread type to be background thread , then this thread is going to be dependent on other foreground thread . Because In the case if none of the thread of type foreground is running any more , then all the background thread will have to be forcefully aborted .




回答6:


If any of the foreground or background threads terminate, the application dies immediately. It is possible to change the thread from foreground to background and vice versa at any time during application lifetime. CLR creates two kinds of threads to better support AppDomain. CLR will forcibly end any background threads that are running if the foreground thread terminates. Any threads created by native code that enter the managed execution environment are marked as background threads.




回答7:


Background thread is going to be killed no matter if it's not finished yet when there will be no active foreground threads.

An example of foreground thread is Application Main thread.

Background thread examples are:

  • System.Threading.Task class
  • System.Threading.ThreadPool class

For more information, check out out this MSDN article.



来源:https://stackoverflow.com/questions/1330585/in-clr-what-is-difference-between-a-background-and-foreground-thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!