For 循环导致 Task.Run 或 Task.Start 溢出

For Loop result in Overflow with Task.Run or Task.Start

遇到问题,希望有人能帮我解决。

我尝试在循环中启动 4 个任务,但我收到 ArgumentOutOfRangeException:

 for (int i = 0; i < 4; i++)
     {
          //start task with current connection
          tasks[i] = Task<byte[]>.Run(() => GetData(i, plcPool[i]));
     }

循环溢出,因为 i = 4

如果我在没有循环的情况下启动任务,它们 运行 没有任何问题:

            tasks[0] = Task<byte[]>.Run(() => GetData(0, plcPool[0]));
            tasks[1] = Task<byte[]>.Run(() => GetData(1, plcPool[1]));
            tasks[2] = Task<byte[]>.Run(() => GetData(2, plcPool[2]));
            tasks[3] = Task<byte[]>.Run(() => GetData(3, plcPool[3]));

不知道为什么?任务 通过套接字连接从西门子 PLC 获取数据。 PLC 最多支持 32 个连接。每个连接我收到 200 字节。

 private byte[] GetData(int id, PLC plc)
    {
        switch (id)
        {
            case 0:
                return plc.ReadBytes(DataType.DataBlock, 50, 0, 200);
            case 1:
                return plc.ReadBytes(DataType.DataBlock, 50, 200, 200);
            case 2:
                return plc.ReadBytes(DataType.DataBlock, 50, 500, 200);
            case 3:
                return plc.ReadBytes(DataType.DataBlock, 50, 700, 200);
            case 4:
                return plc.ReadBytes(DataType.DataBlock, 50, 900, 117);
            default:
                return null;
        }
    }

有什么想法吗?

问候山姆

可能是closure problem造成的。

试试这个:

 for (int i = 0; i < 4; i++)
 {
      //start task with current connection
      int index = i;
      tasks[index] = Task<byte[]>.Run(() => GetData(index, plcPool[index]));
 }

可能发生的情况是,当最后一个线程开始 运行 时,循环已经将 i 增加到 4,这就是传递给 GetData() 的值。将 i 的值捕获到一个单独的变量 index 中并使用它应该可以解决该问题。

举个例子,如果你试试这个代码:

public static void Main()
{
    Console.WriteLine("Starting.");

    for (int i = 0; i < 4; ++i)
        Task.Run(() => Console.WriteLine(i));

    Console.WriteLine("Finished. Press <ENTER> to exit.");
    Console.ReadLine();
}

它经常会给你这样的输出:

Starting.
Finished. Press <ENTER> to exit.
4
4
4
4

将该代码更改为:

public static void Main()
{
    Console.WriteLine("Starting.");

    for (int i = 0; i < 4; ++i)
    {
        int j = i;
        Task.Run(() => Console.WriteLine(j));
    }

    Console.WriteLine("Finished. Press <ENTER> to exit.");
    Console.ReadLine();
}

你会得到类似

的东西
Starting.
Finished. Press <ENTER> to exit.
0
1
3
2

请注意它仍然没有必要按顺序排列!您将看到打印出所有正确的值,但顺序不确定。多线程很棘手!

In C# 5, the loop variable of a foreach will be logically inside the loop, and therefore closures will close over a fresh copy of the variable each time. The "for" loop will not be changed.

另一种方式是:

  1. 创建任务
  2. 运行 任务。
  3. 等待所有任务完成。

演示代码如下:

 internal class Program
{
    private static void Main(string[] args)
    {
        Task[] tasks = new Task[4];

        for (int i = 0; i < 4; i++)
        {
            //start task with current connection
            tasks[i] = new Task<byte[]>(GetData,i);
        }

        foreach (var task in tasks)
        {
            task.Start();
        }

        Task.WaitAll(tasks);

        Console.Read();
    }

    private static byte[] GetData(object index)
    {
        var i = (int) index;
        switch (i)
        {
            case 0:

                //return plc.ReadBytes(DataType.DataBlock, 50, 0, 200);
                Console.WriteLine(i);
                return new byte[] { };
            case 1:
            //return plc.ReadBytes(DataType.DataBlock, 50, 200, 200);
            case 2:
                Console.WriteLine(i);
                return new byte[] { };
            //return plc.ReadBytes(DataType.DataBlock, 50, 500, 200);
            case 3:
                Console.WriteLine(i);
                return new byte[] { };
            //return plc.ReadBytes(DataType.DataBlock, 50, 700, 200);
            case 4:
                //return plc.ReadBytes(DataType.DataBlock, 50, 900, 117);
                Console.WriteLine(i);
                return new byte[] { };

            default:
                return null;
        }
    }
}

输出:

    3  
    1  
    0  
    2  

注意:是 new Task<byte[]>(GetData,i); 不是 new Task<byte[]>(()=>GetData(i));

()=>v means "return the current value of variable v", not "return the value v was back when the delegate was created". Closures close over variables, not over values.

所以new Task<byte[]>(GetData,i);没有“闭包问题

2021 年,您真的应该为此使用内置的 Parallel.For。要达到你想要的效果非常简单:

ConcurrentBag<byte[]> results = new ConcurrentBag<byte[]>();

ParallelLoopResult result = Parallel.For(0, 4, (i, state) => {
   results.Add(GetData(i, plcPool[i]));
});