SoFunction
Updated on 2025-03-07

Introduction to Parallel Class of C# Parallel Library

The function of this function is somewhat similar to Task, which is to execute a series of tasks concurrently and then wait for all to complete. Compared with Task, if this step is omitted, it naturally lacks the related management functions of Task. It comes in two forms:

( params Action[] actions);
(Action[] actions,TaskManager manager,TaskCreationOptions options);

The second way is to customize a TaskManager to manage the task execution thread (the first way is the default).

Examples are as follows:

static void Main(string[] args)
{
    var actions = new Action[]{
        () => ActionTest("test 1"),
        () => ActionTest("test 2"),
        () => ActionTest("test 3"),
        () => ActionTest("test 4")};

    (" 1 Test");
    (actions);

    ();

    (" 2 Test");
    (actions, new TaskManager(new TaskManagerPolicy(1, 1, 2)), );
}

static void ActionTest(object value)
{
    (">>> thread:{0}, value:{1}",
    , value);
}

The output result is as follows:

1 Test
>>> thread:3, value:test 1
>>> thread:3, value:test 2
>>> thread:4, value:test 3
>>> thread:5, value:test 4

2 Test
>>> thread:7, value:test 1
>>> thread:7, value:test 2
>>> thread:8, value:test 3
>>> thread:7, value:test 4

It can be seen that since the second time it is specified that at most two threads can be used to execute, there are only two Tasks that are executed concurrently.

and

And it is more widely used, and it can generate some tasks based on a data source (need to create these tasks) and perform these tasks concurrently. The basic examples are as follows:

static void Main(string[] args)
{
    var data = new object[] { "test 1", "test 2", "test 3" };

    (" Test");
    (data, item => ActionTest(item));

    ();

    (" Test");
    (0, , index => ActionTest(data[index]));
}

These two functions have multiple overload forms and provide many control functions. Since they are not used a lot, I will not introduce them one by one here. But one thing is not very good: if you need to use TaskManager, you have to use the most complicated form. I hope that a more reasonable overloading form will be provided in the final version. After all, TaskManager is still very commonly used (although the current TaskManager function is a bit weak), and those complex parameters are not very commonly used. Therefore, here are two commonly used extension methods encapsulations:

public static class ParallelExtend
{
    public static void ParallelForEach<T>(this IEnumerable<T> source, Action<T> hanlder)
    {
        (source, hanlder);
    }

    public static void ParallelForEach<T>(this IEnumerable<T> source, Action<T> hanlder, TaskManagerPolicy policy)
    {
        using (var manager = new TaskManager(policy))
        {
            (source,
                () => 0,
                (item, index, state) => hanlder(item),
                local => { },
                manager,
                );
        }
    }
}

It is quite convenient to use through the extension method.

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