Task and ThreadPool have similar functions and can be used to create some lightweight parallel tasks. For putting a task into a thread pool
(A);
If this code is implemented using Task, the method is as follows:
(A);
The usage and implementation functions of the code on both ends are very similar. But compared with TheadPool, Task has more functions and is more convenient for us to use.
Suppose we want to create three tasks and wait for them to complete. This function is implemented as follows using TheadPool:
using (ManualResetEvent mre1 = new ManualResetEvent(false)) using (ManualResetEvent mre2 = new ManualResetEvent(false)) using (ManualResetEvent mre3 = new ManualResetEvent(false)) { (delegate { A(); (); }); (delegate { B(); (); }); (delegate { C(); (); }); (new WaitHandle[] { mre1, mre2, mre3 }); }
It is relatively simple to implement with the Task class:
Task t1 = (delegate { A(); }); Task t2 = (delegate { B(); }); Task t3 = (delegate { C(); }); (); (); ();
Or we can write this:
Task t1 = (delegate { A(); }); Task t2 = (delegate { B(); }); Task t3 = (delegate { C(); }); (t1, t2, t3);
Let’s briefly introduce the basic usage of Task:
Create Task
There are two ways to create a Task
- Created through a constructor
Task t1 = new Task(A);
- Created via TaskFactory
Task t1 = (A);
These two methods are actually the same. The first method also introduces the default TaskFactory. TaskFactory plays a role in creating and scheduling management of Tasks, similar to the TaskManager in the previous CTP version. Regarding this object, a separate article will be introduced in the future.
Start running Task
Among the above two ways to create a task, the task created in Method 1 is not executed immediately, and needs to be manually called () to be executed (similar to a thread, which requires manual execution). The Task created by Method 2 is executed immediately (similar to a thread pool, which is executed automatically), and this can be seen from the function names of these two methods.
Wait for Task to complete
There are two types of people waiting for Task to complete:
- Call Task's member function().
- Calls Task's static function () or ().
These two methods are similar to WaitHandle commonly used in .net, so I won't introduce them here.
Cancel Task
The way to cancel the task is much more complicated and powerful than when CTP is used. A separate chapter will be introduced separately in the future.
Exception handling
When an exception occurs during execution of Task, the exception will be re-throwed in functions such as Wait or WaitAll. The exception that occurs can be obtained through the Task's Exception property.
var t1 = (() => { throw new Exception("t1 error occor"); }); var t2 = (() => { throw new Exception("t2 error occor"); }); try { (t1, t2); } catch (Exception) { (); (); }
Get the return value of Task
In the CTP version, the Task with the return value is obtained through the Future<> class. Now the class has been renamed to Task<>, thereby achieving the unification of naming methods. The usage method is almost the same, which means that there is an additional Result attribute, which can obtain the return value after the Task is executed. Examples are as follows:
var t1 = (() => 3); (); ();
other
There are also many very useful methods and attributes for task scheduling and error handling in Task, which make concurrent operations more powerful and simple.
This is all about this article about C# parallel programming Task class. I hope it will be helpful to everyone's learning and I hope everyone will support me more.