A task can also constitute a hierarchy. When a task starts a new task, a hierarchy is started. The following code snippet creates a task in the parent task. The code for creating a subtask is the same as that for creating a parent task. The only difference is that a subtask is created from another task:
1: static void Main(string[] args)
2: {
3: Task parentTask = new Task(ParentTask);
4: parentTask.Start();
5:
6: Console.Read();
7: }
8:
9: static void ParentTask()
10: {
11: Console.WriteLine("Parent task is starting");
12: Task childTask = new Task(ChildTask);
13: childTask.Start();
14: Thread.Sleep(2000);
15: Console.WriteLine("Parent task is finished");
16: }
17:
18: static void ChildTask()
19: {
20: Console.WriteLine("Child task started");
21: Thread.Sleep(4000);
22: Console.WriteLine("Child task is finished");
23: }
Result
In general, there are no new things in hierarchical tasks, but note that the parent task ends before the child task (for example, the parent task is in the WaitingForChildrenToComplete State until all child tasks are completed, the status of the parent task changes to RunToCompletion. If the TaskCreationOptions enumeration is DetectedFromParent when a subtask is created in the parent task, the preceding settings are invalid.
References: C # advanced programming, http://topic.csdn.net/u/20120306/20/24961e41-5bf2-4b81-b45b-3cdd714b12d8.html
Http://www.cnblogs.com/sosowjb/archive/2012/08/12/2635280.html