Nested tasks and subtasks

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

  The task of nested refers to a task and created a task, and the new task, called sub task. In the absence of special announcement, his task is running together, such as the SimpleNestedTask method.

  If you want to let the father and son task associated words, need to create sub task, the parameter TaskCreationOptions.AttachedToParent increases, the father and son relation, so the parent task will wait for the sub task is completed, will be completed.

  If you use Task<TResult> to create a task, it does not require the use of the TaskCreationOptions.AttachedToParent parameter, because as long as the parent task using the sub task returns a result, the parent task will wait for the child to complete the task.

 public class Program
    {
        static void Main(string[] args)
        { 
            WaitForSimpleNestedTask();
            Console.WriteLine("=====================================================");
            SimpleNestedTask();
            Thread.SpinWait(600000);//Waiting for SimpleNestedTask before the end of operation
            Console.WriteLine("=====================================================");
            //SimpleNestedTaskAttachedToParent();
            Console.Read();
        }
        static void WaitForSimpleNestedTask()
        {
            var outer = Task<int>.Factory.StartNew(() =>
            {
                Console.WriteLine("Outer1 task executing.");

                var nested = Task<int>.Factory.StartNew(() =>
                {
                    Console.WriteLine("Nested1 task starting.");
                    Thread.SpinWait(5000000);
                    Console.WriteLine("Nested1 task completing.");
                    return 42;
                });

                // The parent task waiting for the child to complete the task
                return nested.Result;
                // The parent task does not wait for the sub task completion
                //return 1;
            });
            //Output when using the return value of outer.Result, so the automatic waiting parent task completed
            Console.WriteLine("Outer1 has returned {0}.", outer.Result);
        }
        static void SimpleNestedTask()
        {
            var parent = Task.Factory.StartNew(() =>
            {
                Console.WriteLine("Outer2 task executing.");

                var child = Task.Factory.StartNew(() =>
                {
                    Console.WriteLine("Nested2 task starting.");
                    Thread.SpinWait(500000);
                    Console.WriteLine("Nested2 task completing.");
                });
            });
            //Wait for the parent task is completed, then the output string, if not the waiting, this sentence will be the first output
            parent.Wait();
            Console.WriteLine("Outer2 has completed."); 
        }

        static void SimpleNestedTaskAttachedToParent()
        {
            var parent = Task.Factory.StartNew(() =>
            {
                Console.WriteLine("Outer3 task executing.");

                var child = Task.Factory.StartNew(() =>
                {
                    Console.WriteLine("Nested3 task starting.");
                    Thread.SpinWait(500000);
                    Console.WriteLine("Nested3 task completing.");
                }, TaskCreationOptions.AttachedToParent);
                //The parameter TaskCreationOptions.AttachedToParent increases, the sub tasks into an additional parent task, this task is completed, the parent task is completed. 
            });
            
            //Wait for the parent task is completed, then the output string, if not the waiting, this sentence will be the first output
            parent.Wait();
            Console.WriteLine("Outer has completed.");
        }

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Duncan at November 16, 2013 - 2:43 PM