SoFunction
Updated on 2025-03-07

Introduction to C# Parallel Library Task Class

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.

()The function of this function is to wait for multiple tasks to wait for the task to complete, which is often used when thread synchronization.

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);
  • andThis is a member function that cancels the created task. If the task is being executed, the function does not terminate the execution of the task, but only sets the IsCanceled property to true.
    The difference is that in addition to canceling the task, the task will also be waited for the execution of the task to be completed (if the task is in execution).

  • The function of this member function is to perform a subsequent operation after executing the Task. This is also a relatively useful function. Since it is relatively simple, I won't introduce it.

In addition to the methods introduced above, Task also has some more useful properties and methods, which are also very simple. Just refer to its description.

TaskManager and TaskManagerPolicy

These two classes mainly manage Task's policies. The main management attributes include: the minimum number of processors for processing Tasks, the ideal number of processors for processing Tasks, the ideal number of processing threads and priority attributes.

Using TaskManager in Task is very simple, just pass the manager into the constructor when creating the Task. As shown in the following example:

    var manager = new TaskManager(new TaskManagerPolicy(1, 1,1));
    var t1 = (x => (2000), manager);
    var t2 = (x => (2000), manager);

    var start = ;
    (t1, t2);
    var end = ;

    (end-start);

Here I set the number of processing threads to 1. In this way, only one thread will handle the task, and the total time it takes 4 seconds to execute these two tasks; and when using the default manager, these two tasks are executed concurrently and only two seconds can be completed.

Exception handling

When an exception occurs during execution of Task, the exception is re-throwed in the Wait or WaitAll function. The exception that occurs can be obtained through the Task's Exception property.

    var t1 = (x => { throw new Exception("t1 error occor"); });
    var t2 = (x => { throw new Exception("t2 error occor"); });

    try
    {
        (t1, t2);
    }
    catch(Exception)
    {
        ();
        ();
    }

In addition, I will briefly introduce the Future class. The functions and usage of this class are very similar to Task. It can be regarded as a Task with a return value. Examples are as follows:

    var f1 = (() => 3);

    ();
    ();

This is all about this article about the C# parallel library Task class. I hope it will be helpful to everyone's learning and I hope everyone will support me more.