C # Implementation of asynchronous programming (6) -- hierarchical tasks

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.