有问题了,希望有人能帮我。
我试图在循环中启动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]));不知道为什么?任务GetData从西门子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;
}
}知道吗?
问候山姆
发布于 2015-10-22 07:55:35
这可能是由闭包问题引起的。
试试这个:
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请注意,它仍然不一定是有序的!您将看到打印出的所有正确值,但顺序不确定。多线程是很狡猾的!
发布于 2021-05-06 12:26:19
在2021年,您应该只使用内置的Parallel.For。要达到你想要的效果非常简单:
ConcurrentBag<byte[]> results = new ConcurrentBag<byte[]>();
ParallelLoopResult result = Parallel.For(0, 4, (i, state) => {
results.Add(GetData(i, plcPool[i]));
});https://stackoverflow.com/questions/33275831
复制相似问题