95

I'm just wondering whether the new Task class in dot.net 4 is creating a background or foreground thread ?

Normally I'd set "IsBackground" on a Thread, but there's no such attribute on a Task.

I've not been able to find any documentation of this on MSDN :-(

M.Babcock
  • 18,753
  • 6
  • 54
  • 84
Steffen
  • 13,648
  • 7
  • 57
  • 67

5 Answers5

131

Shouldn't be tough to verify:

class Program
{
    static void Main()
    {
        Task
            .Factory
            .StartNew(() => Console.WriteLine(Thread.CurrentThread.IsBackground))
            .Wait();
    }
}

And the answer is ...

ǝnɹʇ

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • I'm having an issue where all my tasks are running on the calling thread. I pasted that precise line of code above into my project and it reports "false". Do you know why that might be? I'm using .NET 4.0. – Trevor Elliott Apr 11 '13 at 17:37
  • 8
    According to this thread http://stackoverflow.com/questions/7889746/creating-threads-task-factory-startnew-vs-new-thread you are not guaranteed a background thread when starting a task, but you should be if you use the TaskCreationOptions.LongRunning. I am using that option and it is still not creating a background thread. What gives? – Trevor Elliott Apr 11 '13 at 17:43
  • 1
    can it be configured not to ? – Royi Namir Jun 21 '13 at 21:11
  • 2
    I believe Moozhe's problem may be with the default synchronization context. The behavior may be different depending on whether your in a WinForms, WPF, Web or Console application since they all have different default ways of scheduling concurrent tasks. – jpierson Jul 24 '13 at 20:23
17

If you are starting a Task<T> using Task.Run(), then yes.

If you are using async and await, then no. Excerpt from here:

"The async and await keywords don't cause additional threads to be created. Async methods don't require multithreading because an async method doesn't run on its own thread. The method runs on the current synchronization context and uses time on the thread only when the method is active. You can use Task.Run to move CPU-bound work to a background thread, but a background thread doesn't help with a process that's just waiting for results to become available."

Steztric
  • 2,832
  • 2
  • 24
  • 43
  • 1
    Since you are using Task.Run it will be on background thread, but if it hits an await then it might do something special to pause the thread and wait for results. Worth investigating. – Steztric Jul 16 '18 at 03:56
4

It appears to run as a background thread.

See this thread:

Running multiple C# Task Async

Community
  • 1
  • 1
Matt Evans
  • 7,113
  • 7
  • 32
  • 64
-1

Tasks are executed by threads which are coming from the system thread pool. A thread that comes from thread pool is executed in background by default.

RotatingWheel
  • 1,023
  • 14
  • 27
-2

If you are not yet convinced of a background task, just try to access a GUI element from within a Task like:

public async Task<int> ProcessStuff_Async()
{
    while(true)
    {
        label1.Text = "processing next item";

to get the run time exception:

Cross-thread operation not valid: Control 'label1' accessed from a thread other than the thread it was created on.

just like with the good old regular background threads.

There is info in MSDN docs (as of 2017 :-) , e.g.:

The best way to handle this ... is to start a background thread which does the work using Task.Run, and await its result. This will allow the UI to feel smooth as the work is being done.

This doc even has a section What happens under the covers.

Roland
  • 4,619
  • 7
  • 49
  • 81
  • Your check only confirms that the task runs in its own thread, not that the thread is a background thread. A background thread is a thread that, if still executing, won't prevent the program from terminating. – Sören Kuklau Apr 10 '18 at 06:12
  • @SörenKuklau or whoever voted me down today: indeed I missed the point that a background thread is a special thread. I am wondering now if the concept of background thread originates from unix or was invented by Windows? – Roland Sep 02 '21 at 08:16
  • @SörenKuklau I added "or whoever" because the downvote was in the last 7 days, while you commented 3 years ago. I don't worry about the downvote but am still curious as to if the background thread that keeps an app from terminating is a windows invention. – Roland Sep 03 '21 at 10:16