21

What is difference between a background and foreground thread ?

Arsen Khachaturyan
  • 7,904
  • 4
  • 42
  • 42
Sandbox
  • 7,910
  • 11
  • 53
  • 67
  • 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
  • 9
    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

7 Answers7

27

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.

Zed
  • 57,028
  • 9
  • 76
  • 100
22

From MSDN:

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

Matthew Jones
  • 25,644
  • 17
  • 102
  • 155
7

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.

Sagar Jaybhay
  • 71
  • 1
  • 2
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 threads. Because In the case if none of the thread of type foreground is running anymore, then all the background thread will have to be forcefully aborted.

MarredCheese
  • 17,541
  • 8
  • 92
  • 91
Vikas Singh
  • 51
  • 1
  • 3
3

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.

V. S.
  • 1,086
  • 14
  • 14
  • you mean, indirectly the priority of background thread is lesser than that of foreground thread? and background thread are at the fate of foreground thread, to let them get a chance? – eRaisedToX Feb 08 '21 at 05:40
1

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.

Arsen Khachaturyan
  • 7,904
  • 4
  • 42
  • 42
0

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.

nihnih
  • 41
  • 1