小编Jam*_*xon的帖子

Parallel.For List <Int32>

我写了以下几行代码:

    private static List<Int32> GetRandomList_Serial()
    {
        List<Int32> returnValue = new List<int>();
        Random random = new Random();

        for (int i = 0; i < 10000000; i++)
        {
            returnValue.Add(random.Next());
        }
        returnValue.Sort();
        return returnValue;
    }
Run Code Online (Sandbox Code Playgroud)

然后我写了这段代码:

    private static List<Int32> GetRandomList_Parallel()
    {
        List<Int32> returnValue = new List<int>();
        Random random = new Random();

        Parallel.For(0, 10000000, y =>
        {
            returnValue.Add(random.Next());
        });

        returnValue.Sort();
        return returnValue;

    }
Run Code Online (Sandbox Code Playgroud)

串口工作正常,并行抛出此异常:

System.ArgumentException未被用户代码处理

目标数组不够长.检查destIndex和length,以及数组的下限.

任何人都知道为什么?

.net task-parallel-library

3
推荐指数
1
解决办法
385
查看次数

标签 统计

.net ×1

task-parallel-library ×1