Parallel Programming-Task Result & Continuation Task, parallelprogramming
This topic describes tasks with returned values and Continuation tasks.
I. demo of Task1.1 code with return values
The previous blog introduced tasks, mainly tasks with no returned values. In this article, tasks with returned values are read through Task. Result.
class TaskFuture { public int Action() { Thread.Sleep(2000); return 1; } public int FutureDemo() { var task = Task.Factory.StartNew<int>(Action); ; return task.Result; } } class Program { static void Main(string[] args) { var stopwatch = Stopwatch.StartNew(); stopwatch.Start(); var result = new TaskFuture().FutureDemo(); Console.WriteLine(result); Console.WriteLine("Finished"); Console.Read(); } }
1.2 Execution Process
When you access task. Result, it is possible that the Task has not started or the Task has not been completed. At this time, the current thread will be blocked until the task is completed and result is returned. Similar to Task. Wait (). When Task. Wait () is called, The Calling thread is blocked until the Task is completed.
Note: If the Task has not started when the Task. Result is called, the Task may not start a new thread, but run directly in the current thread (inline ). It becomes serial.
1.3 running results
The above running result shows the "block" mentioned above.
Ii. Continuation Task
A Continuation Task is a Task that continues to execute other tasks when it is in a certain state.
2.1 code demonstration
class ContinueTask { public int Action() { Console.WriteLine("in Action"); return 1; } public void Continue() { var task = Task.Factory.StartNew<int>(Action); var continueTask = task.ContinueWith<int>(t => { Console.WriteLine("in continue Task"); return 2; }); Console.WriteLine("The result of continie task is " + continueTask.Result); } } class Program { static void Main(string[] args) { var stopwatch = Stopwatch.StartNew(); stopwatch.Start(); new ContinueTask().Continue(); Console.WriteLine("Finished"); Console.Read(); } }
2.2 running results
2.3 practical application
The ContinueTask operation can be canceled when multiple tasks are executed in parallel. Click for details.
Iii. Task. Factory. ContinueWhenAll
ContinueWhenAll is the continuation of multiple tasks. It refers to continuing to execute a Task when multiple tasks are completed.
3.1 code demonstration
class ContinueTask { public int Action1() { Console.WriteLine("in Action1"); return 1; } public int Action2() { Console.WriteLine("in Action2"); return 2; } public void Continue() { var task1 = Task.Factory.StartNew<int>(Action1); var task2 = Task.Factory.StartNew<int>(Action2); var continueTask = Task.Factory.ContinueWhenAll<int>(new Task[] { task1, task2 }, (tasks) => { Console.WriteLine("in Continue Task"); return task1.Result + task2.Result; }); Console.WriteLine("The result of continie task is " + continueTask.Result); } } class Program { static void Main(string[] args) { var stopwatch = Stopwatch.StartNew(); stopwatch.Start(); new ContinueTask().Continue(); Console.WriteLine("Finished"); Console.Read(); } }
The result of Task 1 and Task 2 is also used in the above continuation task.
3.2 running results