For 循环导致 Task.Run 或 Task.Start 溢出 [英] For Loop result in Overflow with Task.Run or Task.Start

查看:27
本文介绍了For 循环导致 Task.Run 或 Task.Start 溢出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有问题,希望有人能帮我.

got a Problem, hope someone can help me out.

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

i try to start 4 Task in an Loop but im getting an ArgumentOutOfRangeException:

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

循环会溢出,因为 i = 4

The Loop gets an Overflow because i = 4

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

if i start the Tasks without a Loop, they run without any Problems:

            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 字节.

dont know why? The Tasks GetData from a Siemens PLC via Socket Connection. The PLC Supports up to 32 Connections. I receive 200 Bytes per Connection.

 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;
        }
    }

有什么想法吗?

问候山姆

推荐答案

这可能是由 闭包问题.

试试这个:

 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 中并使用它应该可以解决这个问题.

What is probably happening is that when the last thread starts running, the loop has already incremented i to 4, and that's the value that gets passed to GetData(). Capturing the value of i into a separate variable index and using that instead should solve that issue.

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

As an example, if you try this code:

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();
}

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

it will often give you this kind of output:

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

请注意它仍然不是必需的!您将看到打印出的所有正确值,但顺序不确定.多线程很棘手!

Note how it is STILL NOT NECESSARILY IN ORDER! You will see all the correct values printed out, but in an indeterminate order. Multithreading is tricksy!

这篇关于For 循环导致 Task.Run 或 Task.Start 溢出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆