Is background thread only executed when the number of foreground threads executing is smaller than the number...












0















According to docs.microsoft.com [1, 2, 3, 4]:




A background thread executes only when the number of foreground threads executing is smaller than the number of processors.




However, if I start 4 foreground threads on a 4-core CPU (without hyper-threading) and then start 4 background threads, the foreground threads and background threads will run side-by-side, which seems to contradict the statement above.



Code sample:



static void Main(string args)
{
int numberOfProcessors = 4;

for (int i = 0; i < numberOfProcessors; i++)
{
int threadNumber = i;

new Thread(() =>
{
Console.WriteLine($"Foreground thread {threadNumber} started.");

for (int j = 1; j <= 100; j++)
{
for (long k = 0; k < 10000000000; k++);

Console.WriteLine($"Foreground thread {threadNumber} progress: {j}%.");
}
})
.Start();
}

for (int i = 0; i < numberOfProcessors; i++)
{
int threadNumber = i;

var backgroundThread = new Thread(() =>
{
Console.WriteLine($"Background thread {threadNumber} started.");

for (int j = 1; j <= 100; j++)
{
for (long k = 0; k < 10000000000; k++);

Console.WriteLine($"Background thread {threadNumber} progress: {j}%.");
}
});
backgroundThread.IsBackground = true;
backgroundThread.Start();
}

Console.ReadLine();
}


Output:



Foreground thread 1 started.
Foreground thread 0 started.
Foreground thread 3 started.
Foreground thread 2 started.
Background thread 0 started.
Background thread 1 started.
Background thread 2 started.
Background thread 3 started.
Foreground thread 2 progress: 1%.
Foreground thread 0 progress: 1%.
Foreground thread 1 progress: 1%.
Foreground thread 3 progress: 1%.
Background thread 1 progress: 1%.
Background thread 0 progress: 1%.
Background thread 2 progress: 1%.
Background thread 3 progress: 1%.
Foreground thread 0 progress: 2%.
Foreground thread 2 progress: 2%.
Foreground thread 1 progress: 2%.
Foreground thread 3 progress: 2%.
Background thread 0 progress: 2%.
Background thread 1 progress: 2%.
Background thread 3 progress: 2%.
Background thread 2 progress: 2%.
Foreground thread 0 progress: 3%.
Foreground thread 2 progress: 3%.
Foreground thread 1 progress: 3%.
Foreground thread 3 progress: 3%.
Background thread 1 progress: 3%.
Background thread 0 progress: 3%.
Background thread 3 progress: 3%.
Background thread 2 progress: 3%.
...


Is the statement incorrect or do I simply get it wrong?










share|improve this question




















  • 1





    The MSDN docs are just blatantly wrong. It does not give a definition of "foreground" vs "background" threads. As far the OS is concerned, a foreground thread is one that has a window that the user is interacting with. It does get a larger thread quantum.

    – Hans Passant
    Nov 15 '18 at 23:25













  • Would this be a 4 core Intel with Hyperthreading? That would give you 8 threads on those 4 cores. Maybe try that again with 8 instead of 4...

    – Patrick Hughes
    Nov 15 '18 at 23:25











  • @Patrick Hughes, no hyper-threading, edited the question, thanks anyway.

    – A. Milto
    Nov 15 '18 at 23:31


















0















According to docs.microsoft.com [1, 2, 3, 4]:




A background thread executes only when the number of foreground threads executing is smaller than the number of processors.




However, if I start 4 foreground threads on a 4-core CPU (without hyper-threading) and then start 4 background threads, the foreground threads and background threads will run side-by-side, which seems to contradict the statement above.



Code sample:



static void Main(string args)
{
int numberOfProcessors = 4;

for (int i = 0; i < numberOfProcessors; i++)
{
int threadNumber = i;

new Thread(() =>
{
Console.WriteLine($"Foreground thread {threadNumber} started.");

for (int j = 1; j <= 100; j++)
{
for (long k = 0; k < 10000000000; k++);

Console.WriteLine($"Foreground thread {threadNumber} progress: {j}%.");
}
})
.Start();
}

for (int i = 0; i < numberOfProcessors; i++)
{
int threadNumber = i;

var backgroundThread = new Thread(() =>
{
Console.WriteLine($"Background thread {threadNumber} started.");

for (int j = 1; j <= 100; j++)
{
for (long k = 0; k < 10000000000; k++);

Console.WriteLine($"Background thread {threadNumber} progress: {j}%.");
}
});
backgroundThread.IsBackground = true;
backgroundThread.Start();
}

Console.ReadLine();
}


Output:



Foreground thread 1 started.
Foreground thread 0 started.
Foreground thread 3 started.
Foreground thread 2 started.
Background thread 0 started.
Background thread 1 started.
Background thread 2 started.
Background thread 3 started.
Foreground thread 2 progress: 1%.
Foreground thread 0 progress: 1%.
Foreground thread 1 progress: 1%.
Foreground thread 3 progress: 1%.
Background thread 1 progress: 1%.
Background thread 0 progress: 1%.
Background thread 2 progress: 1%.
Background thread 3 progress: 1%.
Foreground thread 0 progress: 2%.
Foreground thread 2 progress: 2%.
Foreground thread 1 progress: 2%.
Foreground thread 3 progress: 2%.
Background thread 0 progress: 2%.
Background thread 1 progress: 2%.
Background thread 3 progress: 2%.
Background thread 2 progress: 2%.
Foreground thread 0 progress: 3%.
Foreground thread 2 progress: 3%.
Foreground thread 1 progress: 3%.
Foreground thread 3 progress: 3%.
Background thread 1 progress: 3%.
Background thread 0 progress: 3%.
Background thread 3 progress: 3%.
Background thread 2 progress: 3%.
...


Is the statement incorrect or do I simply get it wrong?










share|improve this question




















  • 1





    The MSDN docs are just blatantly wrong. It does not give a definition of "foreground" vs "background" threads. As far the OS is concerned, a foreground thread is one that has a window that the user is interacting with. It does get a larger thread quantum.

    – Hans Passant
    Nov 15 '18 at 23:25













  • Would this be a 4 core Intel with Hyperthreading? That would give you 8 threads on those 4 cores. Maybe try that again with 8 instead of 4...

    – Patrick Hughes
    Nov 15 '18 at 23:25











  • @Patrick Hughes, no hyper-threading, edited the question, thanks anyway.

    – A. Milto
    Nov 15 '18 at 23:31
















0












0








0








According to docs.microsoft.com [1, 2, 3, 4]:




A background thread executes only when the number of foreground threads executing is smaller than the number of processors.




However, if I start 4 foreground threads on a 4-core CPU (without hyper-threading) and then start 4 background threads, the foreground threads and background threads will run side-by-side, which seems to contradict the statement above.



Code sample:



static void Main(string args)
{
int numberOfProcessors = 4;

for (int i = 0; i < numberOfProcessors; i++)
{
int threadNumber = i;

new Thread(() =>
{
Console.WriteLine($"Foreground thread {threadNumber} started.");

for (int j = 1; j <= 100; j++)
{
for (long k = 0; k < 10000000000; k++);

Console.WriteLine($"Foreground thread {threadNumber} progress: {j}%.");
}
})
.Start();
}

for (int i = 0; i < numberOfProcessors; i++)
{
int threadNumber = i;

var backgroundThread = new Thread(() =>
{
Console.WriteLine($"Background thread {threadNumber} started.");

for (int j = 1; j <= 100; j++)
{
for (long k = 0; k < 10000000000; k++);

Console.WriteLine($"Background thread {threadNumber} progress: {j}%.");
}
});
backgroundThread.IsBackground = true;
backgroundThread.Start();
}

Console.ReadLine();
}


Output:



Foreground thread 1 started.
Foreground thread 0 started.
Foreground thread 3 started.
Foreground thread 2 started.
Background thread 0 started.
Background thread 1 started.
Background thread 2 started.
Background thread 3 started.
Foreground thread 2 progress: 1%.
Foreground thread 0 progress: 1%.
Foreground thread 1 progress: 1%.
Foreground thread 3 progress: 1%.
Background thread 1 progress: 1%.
Background thread 0 progress: 1%.
Background thread 2 progress: 1%.
Background thread 3 progress: 1%.
Foreground thread 0 progress: 2%.
Foreground thread 2 progress: 2%.
Foreground thread 1 progress: 2%.
Foreground thread 3 progress: 2%.
Background thread 0 progress: 2%.
Background thread 1 progress: 2%.
Background thread 3 progress: 2%.
Background thread 2 progress: 2%.
Foreground thread 0 progress: 3%.
Foreground thread 2 progress: 3%.
Foreground thread 1 progress: 3%.
Foreground thread 3 progress: 3%.
Background thread 1 progress: 3%.
Background thread 0 progress: 3%.
Background thread 3 progress: 3%.
Background thread 2 progress: 3%.
...


Is the statement incorrect or do I simply get it wrong?










share|improve this question
















According to docs.microsoft.com [1, 2, 3, 4]:




A background thread executes only when the number of foreground threads executing is smaller than the number of processors.




However, if I start 4 foreground threads on a 4-core CPU (without hyper-threading) and then start 4 background threads, the foreground threads and background threads will run side-by-side, which seems to contradict the statement above.



Code sample:



static void Main(string args)
{
int numberOfProcessors = 4;

for (int i = 0; i < numberOfProcessors; i++)
{
int threadNumber = i;

new Thread(() =>
{
Console.WriteLine($"Foreground thread {threadNumber} started.");

for (int j = 1; j <= 100; j++)
{
for (long k = 0; k < 10000000000; k++);

Console.WriteLine($"Foreground thread {threadNumber} progress: {j}%.");
}
})
.Start();
}

for (int i = 0; i < numberOfProcessors; i++)
{
int threadNumber = i;

var backgroundThread = new Thread(() =>
{
Console.WriteLine($"Background thread {threadNumber} started.");

for (int j = 1; j <= 100; j++)
{
for (long k = 0; k < 10000000000; k++);

Console.WriteLine($"Background thread {threadNumber} progress: {j}%.");
}
});
backgroundThread.IsBackground = true;
backgroundThread.Start();
}

Console.ReadLine();
}


Output:



Foreground thread 1 started.
Foreground thread 0 started.
Foreground thread 3 started.
Foreground thread 2 started.
Background thread 0 started.
Background thread 1 started.
Background thread 2 started.
Background thread 3 started.
Foreground thread 2 progress: 1%.
Foreground thread 0 progress: 1%.
Foreground thread 1 progress: 1%.
Foreground thread 3 progress: 1%.
Background thread 1 progress: 1%.
Background thread 0 progress: 1%.
Background thread 2 progress: 1%.
Background thread 3 progress: 1%.
Foreground thread 0 progress: 2%.
Foreground thread 2 progress: 2%.
Foreground thread 1 progress: 2%.
Foreground thread 3 progress: 2%.
Background thread 0 progress: 2%.
Background thread 1 progress: 2%.
Background thread 3 progress: 2%.
Background thread 2 progress: 2%.
Foreground thread 0 progress: 3%.
Foreground thread 2 progress: 3%.
Foreground thread 1 progress: 3%.
Foreground thread 3 progress: 3%.
Background thread 1 progress: 3%.
Background thread 0 progress: 3%.
Background thread 3 progress: 3%.
Background thread 2 progress: 3%.
...


Is the statement incorrect or do I simply get it wrong?







c# .net multithreading clr






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 17 '18 at 16:09









Markus Safar

4,76241938




4,76241938










asked Nov 15 '18 at 23:18









A. MiltoA. Milto

1,4081413




1,4081413








  • 1





    The MSDN docs are just blatantly wrong. It does not give a definition of "foreground" vs "background" threads. As far the OS is concerned, a foreground thread is one that has a window that the user is interacting with. It does get a larger thread quantum.

    – Hans Passant
    Nov 15 '18 at 23:25













  • Would this be a 4 core Intel with Hyperthreading? That would give you 8 threads on those 4 cores. Maybe try that again with 8 instead of 4...

    – Patrick Hughes
    Nov 15 '18 at 23:25











  • @Patrick Hughes, no hyper-threading, edited the question, thanks anyway.

    – A. Milto
    Nov 15 '18 at 23:31
















  • 1





    The MSDN docs are just blatantly wrong. It does not give a definition of "foreground" vs "background" threads. As far the OS is concerned, a foreground thread is one that has a window that the user is interacting with. It does get a larger thread quantum.

    – Hans Passant
    Nov 15 '18 at 23:25













  • Would this be a 4 core Intel with Hyperthreading? That would give you 8 threads on those 4 cores. Maybe try that again with 8 instead of 4...

    – Patrick Hughes
    Nov 15 '18 at 23:25











  • @Patrick Hughes, no hyper-threading, edited the question, thanks anyway.

    – A. Milto
    Nov 15 '18 at 23:31










1




1





The MSDN docs are just blatantly wrong. It does not give a definition of "foreground" vs "background" threads. As far the OS is concerned, a foreground thread is one that has a window that the user is interacting with. It does get a larger thread quantum.

– Hans Passant
Nov 15 '18 at 23:25







The MSDN docs are just blatantly wrong. It does not give a definition of "foreground" vs "background" threads. As far the OS is concerned, a foreground thread is one that has a window that the user is interacting with. It does get a larger thread quantum.

– Hans Passant
Nov 15 '18 at 23:25















Would this be a 4 core Intel with Hyperthreading? That would give you 8 threads on those 4 cores. Maybe try that again with 8 instead of 4...

– Patrick Hughes
Nov 15 '18 at 23:25





Would this be a 4 core Intel with Hyperthreading? That would give you 8 threads on those 4 cores. Maybe try that again with 8 instead of 4...

– Patrick Hughes
Nov 15 '18 at 23:25













@Patrick Hughes, no hyper-threading, edited the question, thanks anyway.

– A. Milto
Nov 15 '18 at 23:31







@Patrick Hughes, no hyper-threading, edited the question, thanks anyway.

– A. Milto
Nov 15 '18 at 23:31














1 Answer
1






active

oldest

votes


















2














In my opinion the statement is incorrect and in addition you are looking not at the lastest version of the documentation. If you lookup the lastest version of the documentation about Managed Threading / Foreground and Background Threads it states:




A managed thread is either a background thread or a foreground thread. Background threads are identical to foreground threads with one exception: a background thread does not keep the managed execution environment running. Once all foreground threads have been stopped in a managed process (where the .exe file is a managed assembly), the system stops all background threads and shuts down.



Note



When the runtime stops a background thread because the process is shutting down, no exception is thrown in the thread. However, when threads are stopped because the AppDomain.Unload method unloads the application domain, a ThreadAbortException is thrown in both foreground and background threads.



Use the Thread.IsBackground property to determine whether a thread is a background or a foreground thread, or to change its status. A thread can be changed to a background thread at any time by setting its IsBackground property to true.



Important



The foreground or background status of a thread does not affect the outcome of an unhandled exception in the thread. In the .NET Framework version 2.0, an unhandled exception in either foreground or background threads results in termination of the application. See Exceptions in Managed Threads.



Threads that belong to the managed thread pool (that is, threads whose IsThreadPoolThread property is true) are background threads. All threads that enter the managed execution environment from unmanaged code are marked as background threads. All threads generated by creating and starting a new Thread object are by default foreground threads.



If you use a thread to monitor an activity, such as a socket connection, set its IsBackground property to true so that the thread does not prevent your process from terminating.







The same goes for the documentation of the Thread.IsBackground property:




Remarks



A thread is either a background thread or a foreground thread. Background threads are identical to foreground threads, except that background threads do not prevent a process from terminating. Once all foreground threads belonging to a process have terminated, the common language runtime ends the process. Any remaining background threads are stopped and do not complete.



By default, the following threads execute in the foreground (that is, their IsBackground property returns false):




  • The primary thread (or main application thread).

  • All threads created by calling a Thread class constructor.


By default, the following threads execute in the background (that is, their IsBackground property returns true):





  • Thread pool threads, which are a pool of worker threads maintained by the runtime. You can configure the thread pool and schedule work on thread pool threads by using the ThreadPool class.



    Note



    Task-based asynchronous operations automatically execute on thread pool threads.



  • All threads that enter the managed execution environment from unmanaged code.








share|improve this answer


























  • The docs are hardly old: the latest version of the documentation in English (15 Oct, 2018) refers to the article which contains the quoted fragment, the version of documentation before that (5 Apr, 2018) contains that exact quoted fragment, the most recent versions of documentation in Frech, German and Russian (30 Nov, 2017) all contain the quoted fragment.

    – A. Milto
    Nov 16 '18 at 23:28








  • 1





    @A.Milto: 3 of the links in the question you provided reference old versions of the framework (1.1, 3.0, 4.0). The link from your comment references a source from end of 2017 (via web.archive.org), the same original link is already updated. Also take into consideration that the docs will not be updated on a daily basis - they just migrated them to docs.microsoft.com (which probably updated the timestamp).

    – Markus Safar
    Nov 17 '18 at 0:35











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329221%2fis-background-thread-only-executed-when-the-number-of-foreground-threads-executi%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














In my opinion the statement is incorrect and in addition you are looking not at the lastest version of the documentation. If you lookup the lastest version of the documentation about Managed Threading / Foreground and Background Threads it states:




A managed thread is either a background thread or a foreground thread. Background threads are identical to foreground threads with one exception: a background thread does not keep the managed execution environment running. Once all foreground threads have been stopped in a managed process (where the .exe file is a managed assembly), the system stops all background threads and shuts down.



Note



When the runtime stops a background thread because the process is shutting down, no exception is thrown in the thread. However, when threads are stopped because the AppDomain.Unload method unloads the application domain, a ThreadAbortException is thrown in both foreground and background threads.



Use the Thread.IsBackground property to determine whether a thread is a background or a foreground thread, or to change its status. A thread can be changed to a background thread at any time by setting its IsBackground property to true.



Important



The foreground or background status of a thread does not affect the outcome of an unhandled exception in the thread. In the .NET Framework version 2.0, an unhandled exception in either foreground or background threads results in termination of the application. See Exceptions in Managed Threads.



Threads that belong to the managed thread pool (that is, threads whose IsThreadPoolThread property is true) are background threads. All threads that enter the managed execution environment from unmanaged code are marked as background threads. All threads generated by creating and starting a new Thread object are by default foreground threads.



If you use a thread to monitor an activity, such as a socket connection, set its IsBackground property to true so that the thread does not prevent your process from terminating.







The same goes for the documentation of the Thread.IsBackground property:




Remarks



A thread is either a background thread or a foreground thread. Background threads are identical to foreground threads, except that background threads do not prevent a process from terminating. Once all foreground threads belonging to a process have terminated, the common language runtime ends the process. Any remaining background threads are stopped and do not complete.



By default, the following threads execute in the foreground (that is, their IsBackground property returns false):




  • The primary thread (or main application thread).

  • All threads created by calling a Thread class constructor.


By default, the following threads execute in the background (that is, their IsBackground property returns true):





  • Thread pool threads, which are a pool of worker threads maintained by the runtime. You can configure the thread pool and schedule work on thread pool threads by using the ThreadPool class.



    Note



    Task-based asynchronous operations automatically execute on thread pool threads.



  • All threads that enter the managed execution environment from unmanaged code.








share|improve this answer


























  • The docs are hardly old: the latest version of the documentation in English (15 Oct, 2018) refers to the article which contains the quoted fragment, the version of documentation before that (5 Apr, 2018) contains that exact quoted fragment, the most recent versions of documentation in Frech, German and Russian (30 Nov, 2017) all contain the quoted fragment.

    – A. Milto
    Nov 16 '18 at 23:28








  • 1





    @A.Milto: 3 of the links in the question you provided reference old versions of the framework (1.1, 3.0, 4.0). The link from your comment references a source from end of 2017 (via web.archive.org), the same original link is already updated. Also take into consideration that the docs will not be updated on a daily basis - they just migrated them to docs.microsoft.com (which probably updated the timestamp).

    – Markus Safar
    Nov 17 '18 at 0:35
















2














In my opinion the statement is incorrect and in addition you are looking not at the lastest version of the documentation. If you lookup the lastest version of the documentation about Managed Threading / Foreground and Background Threads it states:




A managed thread is either a background thread or a foreground thread. Background threads are identical to foreground threads with one exception: a background thread does not keep the managed execution environment running. Once all foreground threads have been stopped in a managed process (where the .exe file is a managed assembly), the system stops all background threads and shuts down.



Note



When the runtime stops a background thread because the process is shutting down, no exception is thrown in the thread. However, when threads are stopped because the AppDomain.Unload method unloads the application domain, a ThreadAbortException is thrown in both foreground and background threads.



Use the Thread.IsBackground property to determine whether a thread is a background or a foreground thread, or to change its status. A thread can be changed to a background thread at any time by setting its IsBackground property to true.



Important



The foreground or background status of a thread does not affect the outcome of an unhandled exception in the thread. In the .NET Framework version 2.0, an unhandled exception in either foreground or background threads results in termination of the application. See Exceptions in Managed Threads.



Threads that belong to the managed thread pool (that is, threads whose IsThreadPoolThread property is true) are background threads. All threads that enter the managed execution environment from unmanaged code are marked as background threads. All threads generated by creating and starting a new Thread object are by default foreground threads.



If you use a thread to monitor an activity, such as a socket connection, set its IsBackground property to true so that the thread does not prevent your process from terminating.







The same goes for the documentation of the Thread.IsBackground property:




Remarks



A thread is either a background thread or a foreground thread. Background threads are identical to foreground threads, except that background threads do not prevent a process from terminating. Once all foreground threads belonging to a process have terminated, the common language runtime ends the process. Any remaining background threads are stopped and do not complete.



By default, the following threads execute in the foreground (that is, their IsBackground property returns false):




  • The primary thread (or main application thread).

  • All threads created by calling a Thread class constructor.


By default, the following threads execute in the background (that is, their IsBackground property returns true):





  • Thread pool threads, which are a pool of worker threads maintained by the runtime. You can configure the thread pool and schedule work on thread pool threads by using the ThreadPool class.



    Note



    Task-based asynchronous operations automatically execute on thread pool threads.



  • All threads that enter the managed execution environment from unmanaged code.








share|improve this answer


























  • The docs are hardly old: the latest version of the documentation in English (15 Oct, 2018) refers to the article which contains the quoted fragment, the version of documentation before that (5 Apr, 2018) contains that exact quoted fragment, the most recent versions of documentation in Frech, German and Russian (30 Nov, 2017) all contain the quoted fragment.

    – A. Milto
    Nov 16 '18 at 23:28








  • 1





    @A.Milto: 3 of the links in the question you provided reference old versions of the framework (1.1, 3.0, 4.0). The link from your comment references a source from end of 2017 (via web.archive.org), the same original link is already updated. Also take into consideration that the docs will not be updated on a daily basis - they just migrated them to docs.microsoft.com (which probably updated the timestamp).

    – Markus Safar
    Nov 17 '18 at 0:35














2












2








2







In my opinion the statement is incorrect and in addition you are looking not at the lastest version of the documentation. If you lookup the lastest version of the documentation about Managed Threading / Foreground and Background Threads it states:




A managed thread is either a background thread or a foreground thread. Background threads are identical to foreground threads with one exception: a background thread does not keep the managed execution environment running. Once all foreground threads have been stopped in a managed process (where the .exe file is a managed assembly), the system stops all background threads and shuts down.



Note



When the runtime stops a background thread because the process is shutting down, no exception is thrown in the thread. However, when threads are stopped because the AppDomain.Unload method unloads the application domain, a ThreadAbortException is thrown in both foreground and background threads.



Use the Thread.IsBackground property to determine whether a thread is a background or a foreground thread, or to change its status. A thread can be changed to a background thread at any time by setting its IsBackground property to true.



Important



The foreground or background status of a thread does not affect the outcome of an unhandled exception in the thread. In the .NET Framework version 2.0, an unhandled exception in either foreground or background threads results in termination of the application. See Exceptions in Managed Threads.



Threads that belong to the managed thread pool (that is, threads whose IsThreadPoolThread property is true) are background threads. All threads that enter the managed execution environment from unmanaged code are marked as background threads. All threads generated by creating and starting a new Thread object are by default foreground threads.



If you use a thread to monitor an activity, such as a socket connection, set its IsBackground property to true so that the thread does not prevent your process from terminating.







The same goes for the documentation of the Thread.IsBackground property:




Remarks



A thread is either a background thread or a foreground thread. Background threads are identical to foreground threads, except that background threads do not prevent a process from terminating. Once all foreground threads belonging to a process have terminated, the common language runtime ends the process. Any remaining background threads are stopped and do not complete.



By default, the following threads execute in the foreground (that is, their IsBackground property returns false):




  • The primary thread (or main application thread).

  • All threads created by calling a Thread class constructor.


By default, the following threads execute in the background (that is, their IsBackground property returns true):





  • Thread pool threads, which are a pool of worker threads maintained by the runtime. You can configure the thread pool and schedule work on thread pool threads by using the ThreadPool class.



    Note



    Task-based asynchronous operations automatically execute on thread pool threads.



  • All threads that enter the managed execution environment from unmanaged code.








share|improve this answer















In my opinion the statement is incorrect and in addition you are looking not at the lastest version of the documentation. If you lookup the lastest version of the documentation about Managed Threading / Foreground and Background Threads it states:




A managed thread is either a background thread or a foreground thread. Background threads are identical to foreground threads with one exception: a background thread does not keep the managed execution environment running. Once all foreground threads have been stopped in a managed process (where the .exe file is a managed assembly), the system stops all background threads and shuts down.



Note



When the runtime stops a background thread because the process is shutting down, no exception is thrown in the thread. However, when threads are stopped because the AppDomain.Unload method unloads the application domain, a ThreadAbortException is thrown in both foreground and background threads.



Use the Thread.IsBackground property to determine whether a thread is a background or a foreground thread, or to change its status. A thread can be changed to a background thread at any time by setting its IsBackground property to true.



Important



The foreground or background status of a thread does not affect the outcome of an unhandled exception in the thread. In the .NET Framework version 2.0, an unhandled exception in either foreground or background threads results in termination of the application. See Exceptions in Managed Threads.



Threads that belong to the managed thread pool (that is, threads whose IsThreadPoolThread property is true) are background threads. All threads that enter the managed execution environment from unmanaged code are marked as background threads. All threads generated by creating and starting a new Thread object are by default foreground threads.



If you use a thread to monitor an activity, such as a socket connection, set its IsBackground property to true so that the thread does not prevent your process from terminating.







The same goes for the documentation of the Thread.IsBackground property:




Remarks



A thread is either a background thread or a foreground thread. Background threads are identical to foreground threads, except that background threads do not prevent a process from terminating. Once all foreground threads belonging to a process have terminated, the common language runtime ends the process. Any remaining background threads are stopped and do not complete.



By default, the following threads execute in the foreground (that is, their IsBackground property returns false):




  • The primary thread (or main application thread).

  • All threads created by calling a Thread class constructor.


By default, the following threads execute in the background (that is, their IsBackground property returns true):





  • Thread pool threads, which are a pool of worker threads maintained by the runtime. You can configure the thread pool and schedule work on thread pool threads by using the ThreadPool class.



    Note



    Task-based asynchronous operations automatically execute on thread pool threads.



  • All threads that enter the managed execution environment from unmanaged code.









share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 17 '18 at 16:08

























answered Nov 16 '18 at 0:51









Markus SafarMarkus Safar

4,76241938




4,76241938













  • The docs are hardly old: the latest version of the documentation in English (15 Oct, 2018) refers to the article which contains the quoted fragment, the version of documentation before that (5 Apr, 2018) contains that exact quoted fragment, the most recent versions of documentation in Frech, German and Russian (30 Nov, 2017) all contain the quoted fragment.

    – A. Milto
    Nov 16 '18 at 23:28








  • 1





    @A.Milto: 3 of the links in the question you provided reference old versions of the framework (1.1, 3.0, 4.0). The link from your comment references a source from end of 2017 (via web.archive.org), the same original link is already updated. Also take into consideration that the docs will not be updated on a daily basis - they just migrated them to docs.microsoft.com (which probably updated the timestamp).

    – Markus Safar
    Nov 17 '18 at 0:35



















  • The docs are hardly old: the latest version of the documentation in English (15 Oct, 2018) refers to the article which contains the quoted fragment, the version of documentation before that (5 Apr, 2018) contains that exact quoted fragment, the most recent versions of documentation in Frech, German and Russian (30 Nov, 2017) all contain the quoted fragment.

    – A. Milto
    Nov 16 '18 at 23:28








  • 1





    @A.Milto: 3 of the links in the question you provided reference old versions of the framework (1.1, 3.0, 4.0). The link from your comment references a source from end of 2017 (via web.archive.org), the same original link is already updated. Also take into consideration that the docs will not be updated on a daily basis - they just migrated them to docs.microsoft.com (which probably updated the timestamp).

    – Markus Safar
    Nov 17 '18 at 0:35

















The docs are hardly old: the latest version of the documentation in English (15 Oct, 2018) refers to the article which contains the quoted fragment, the version of documentation before that (5 Apr, 2018) contains that exact quoted fragment, the most recent versions of documentation in Frech, German and Russian (30 Nov, 2017) all contain the quoted fragment.

– A. Milto
Nov 16 '18 at 23:28







The docs are hardly old: the latest version of the documentation in English (15 Oct, 2018) refers to the article which contains the quoted fragment, the version of documentation before that (5 Apr, 2018) contains that exact quoted fragment, the most recent versions of documentation in Frech, German and Russian (30 Nov, 2017) all contain the quoted fragment.

– A. Milto
Nov 16 '18 at 23:28






1




1





@A.Milto: 3 of the links in the question you provided reference old versions of the framework (1.1, 3.0, 4.0). The link from your comment references a source from end of 2017 (via web.archive.org), the same original link is already updated. Also take into consideration that the docs will not be updated on a daily basis - they just migrated them to docs.microsoft.com (which probably updated the timestamp).

– Markus Safar
Nov 17 '18 at 0:35





@A.Milto: 3 of the links in the question you provided reference old versions of the framework (1.1, 3.0, 4.0). The link from your comment references a source from end of 2017 (via web.archive.org), the same original link is already updated. Also take into consideration that the docs will not be updated on a daily basis - they just migrated them to docs.microsoft.com (which probably updated the timestamp).

– Markus Safar
Nov 17 '18 at 0:35




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329221%2fis-background-thread-only-executed-when-the-number-of-foreground-threads-executi%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

The Sandy Post

Danny Elfman

Pages that link to "Head v. Amoskeag Manufacturing Co."