One of the differences between Task.Run
and TaskFactory.StartNew
is the addition of the new DenyChildAttach
option by default. But AttachedToParent
was created for a reason. In what situation would you want to use attached child tasks?
The use case for AttachedToParent
is when you have a nested dynamic task parallelism scenario. That is:
Since the vast majority of concurrency problems are I/O-based (not CPU-based), and since the vast majority of parallelism scenarios are data-based parallelism (not dynamic task parallelism), and since dynamic task parallel problems may or may not have a hierarchical nature, this scenario almost never comes up.
Unfortunately, there is a logical parent/child relationship between tasks (including asynchronous tasks), which have caused a lot of developers to incorrectly attempt to use the AttachedToParent
flag with async
tasks. Thus, the introduction of the DenyChildAttach
flag (which prevents AttachedToParent
from taking effect).
The only situation I can think of is made obsolete by async-await. E.g. if you want a task to wait on its child tasks you will await
them.
var parentTask = Task.Run(async () =>
{
await Task.Run(() => Thread.Sleep(1000));
Console.WriteLine("parent task completed");
});
But in .Net 4.0 you would have to Wait()
on them. E.g.
var parentTask = Task.Factory.StartNew(() =>
{
Task.Factory.StartNew(() => Thread.Sleep(1000)).Wait();
Console.WriteLine("parent task completed");
});
Unlike the first example this will block the thread until the child task is complete. With attached tasks we can get the same behaviour like this.
var parentTask = Task.Factory.StartNew(() =>
{
Task.Factory.StartNew(() => Thread.Sleep(1000), TaskCreationOptions.AttachedToParent)
.ContinueWith(antecedent => Console.WriteLine("parent task completed", TaskContinuationOptions.AttachedToParent);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With