Is a new task running in the ThreadPool thread?

This is probably an easy and dumb question. I create a task as follows:

Task<bool> myTask = new Task<bool>(() => { Debug.WriteLine("Task fired"); return true; });
// I know I can create it with Task.Run, but this is for purpose of the sample
myTask.Start(); 

and I have few questions about this:

  • Does it always work in ThreadPool thread?
  • If it runs on ThreadPool, is it likely that it will be launched by the user interface thread? And in the case of working with bey - can block it?
  • In the case of a large number of tasks, can several be assigned to one thread (in the queue), and then run one after another? Or does each task have its own thread?

I read some documentation, but I did not find specific explanations. For example, Task Documentation usually talks about tasks:

Since the work performed by the Task object is usually performed asynchronously in the thread pool thread, and not synchronously in the main application thread ...

+4
source share
2 answers

Does it always work in ThreadPool thread?

Not necessary. If you look at constructor overloading with , you can pass a value . If it uses internally , this will create a new thread that is not one of the threadpool threads. TaskTaskCreationOptionsTaskCreationOptions.LongRunningTaskScheduler.Default

, Task.Run . TaskScheduler, Task.Factory.StartNew, , .

ThreadPool, , ? bey - ?

. , ThreadPool.

, ( ), ? ?

, TaskScheduler. ThreadPoolTaskScheduler ( , - ), threadpool , . , . TaskScheduler, exectuion.

+8

: " .NET Framework 4, - (TPL). [ ], , Task Task, ."

https://msdn.microsoft.com/en-us/library/0ka9477y(v=vs.110).aspx

0

Source: https://habr.com/ru/post/1619025/


All Articles