Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

foreground threads vs background threads

MSDN states that:

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

  1. Is there any dereference in the Thread Quantum that given by OS to the thread if it were a background thread rather than the foreground threads? Check this.

  2. When setting a background thread priority to a higher value like Highest, will it get more quantum than a foreground one with lower priority?

Thanks in advance.

like image 233
Jalal Said Avatar asked May 06 '11 22:05

Jalal Said


People also ask

What is a foreground thread?

Foreground Thread Foreground threads are those threads that keep running even after the application exits or quits. It has the ability to prevent the current application from terminating. The CLR will not shut down the application until all Foreground Threads have stopped.

Which thread is executed in the background?

Communicating with the main thread We can do that by using callbacks. In this example, the callback is executed in the calling thread, which is a background thread.

Is the main thread a foreground thread?

The . NET Framework defines two types of threads: foreground and background. All processes have at least one thread of execution, which is usually called the main thread because it is the one that is executed when your program begins. Is this main thread is back ground or foreground thread.

Which threads execute in the 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.


1 Answers

Managed foreground/background threads, the Windows thread quantum and thread priority are orthogonal concepts.

  • Being a foreground thread means that the process cannot be stopped until it (and all other foreground threads) are stopped.
  • The quantum is the length of time a thread gets to run before the system will consider scheduling another thread on that processor.
  • The thread priority determines the pecking order within all ready to run, waiting threads.

Of these three items, only foreground/background pertains to managed code.

Now to your questions.

  1. The foreground process is a Windows concept. It is unrelated to foreground/background threads in .net.
  2. Thread priority does not affect the quantum.
like image 124
David Heffernan Avatar answered Sep 16 '22 12:09

David Heffernan