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


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



What is difference between a background and foreground thread ?





Clarification: C# has no concept of background and foreground threads. The distinctions that certain scenarios make about a thread are done at a CLR / COM level.
– JaredPar
Aug 25 '09 at 20:02





C# doesn't have the concept of a Giraffe either, but you can make a class with a property called Giraffe. IsBackground is a property of an object in the framework. Remember, C# the language is not the .NET framework. It's just a programming language.
– Eric Lippert
Aug 25 '09 at 20:11




5 Answers
5



From MSDN:



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





You mean that it cannon prevent a current process from terminating...
– Guffa
Aug 25 '09 at 20:04





@Guffa: I don't understand. You're quibbling over an interpretation of the MSDN documentation?
– Amy
Aug 25 '09 at 20:16





Doesn't it have a lower context switching priority also?
– Vince Panuccio
Aug 30 '11 at 9:10



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.





when people should use background thread.....give one good example.
– Mou
Mar 25 '15 at 13:45



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.



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.



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.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Render GeoTiff to on browser with leaflet

How to get chrome logged in user's email id through website

using states in a react-navigation without redux