There was a problem, I hope someone can help me.
I am trying to run 4 Task in 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])); }
Loop gets overflow because i = 4
if I run tasks without a loop, they start without 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]));
Do not know why? GetData Tasks from Siemens PLC via Socket Connection. PLC supports up to 32 connections. I get 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; } }
any idea?
Regards Sam