The description of AttachedToParent states
AttachedToParent: Specifies that the continuation, if it is a child task, is attached to a parent in the task hierarchy. The continuation can be a child task only if its antecedent is also a child task.
What does the emphasized part mean? Does the following code contradict this statement?
static void Example()
{
Task antecedent = new Task(() =>
{
Console.WriteLine("Antecedent begun");
Thread.Sleep(1000);
});
antecedent.Start();
Task parent = null;
parent = new Task(() =>
{
Console.WriteLine("Parent begun");
Thread.Sleep(500);
var continuation = Task.Factory.ContinueWhenAll(new[] { antecedent }, _ =>
{
Thread.Sleep(2000);
Console.WriteLine("parent status: {0}", parent.Status);
}, TaskContinuationOptions.AttachedToParent);
});
parent.Start();
parent.Wait();
}
When I run this, it gives this output:
Antecedent begun
Parent begun
Parent status: WaitingForChildrenToComplete
The continuation task does appear to have been attached to the parent. According to the documentation, to be attached, it must be a child task. But antecedent isn't a child task.