列表<任务<结果>&GT;所有任务都具有相同的结果

时间:2015-06-18 15:44:59

标签: c# list task

我是C#的新手,我必须为我的教育考试写一个小项目。

我必须在一段时间(500毫秒)内通过telnet读取一些传感器 因此,我是通过使用task类来完成此任务的 我在每个周期生成一个任务列表,等到它们准备就绪(超时之后我通过使用令牌取消它们),然后我读出结果。
但是所有结果都是一样的,也许它们都是第一个或最后一个任务,它正在响应。

这是我的代码:

do
            {
                finished = false;
                activcnt = 0;
                tskcnt = 0;
                cts = new CancellationTokenSource();
                CancellationToken ct = cts.Token;
/// generate Tasks               
                messcnt++;
                val.mscount = messcnt;
                sw.Start();
                for (int i = 0; i < arrlengt; i++)
                {
                    if (clients[i] != null && clients[i].Connected && 
                        (parent._swork.lstSensor[i].sensState==1)  ) 
                    {
                        task2arr[tskcnt] = parent._swork.lstSensor[i].sensIdx;                    
                        tasks.Add(Messung(clients[i], parent._swork.lstSensor[i], val, ct));                        
                    }
                }
 /// wait until tasks are running     
               try {
                   if (!Task.WaitAll(tasks.ToArray(), 400))
                   {
                       cts.Cancel();
                   }
                   else
                   {
                       finished = true;
                   }
                }  catch (Exc.. e){..}          
                   time = (sw.ElapsedMilliseconds);
 /// result to list
                var results = new List<Values>(6);
                   foreach (var item in tasks)
                   {
                       results.Add(item.Result);
                   } 
                }
                _output.LogOutput(results);

                CheckErrorCnt(arrlengt, errcnt);

                tasks.Clear();
                time = (intervall - sw.ElapsedMilliseconds);
                Thread.Sleep((int)time);
                sw.Stop();                
                sw.Reset();
            }
           while (parent._gblset.measurestart == true);


private  Task<Values> Messung(TcpClient client , Sensor sens , Values val, CancellationToken ct )
        {
            string msg;

                return Task.Run(() =>
                {
                    int mscnt = val.mscount;                    
                    var _con = new Connecting();

                    if (ct.IsCancellationRequested)
                    {
                        msg= sens.name+" : No response within specified time";
                        val.errstring = msg;
                        return val;
                    }

                   string response = _con.GetMeasure(client, ct);

                   val = parent._calc.GetValues(response, sens.kennIdx);
                   val.sensname = sens.name;
                   val.channame = sens.description;
                   val.sensidx = sens.sensIdx;
                   val.mscount = mscnt;

                   if (ct.IsCancellationRequested)
                   {
                       msg = "Error: " + sens.name + " : No response within specified time";
                       val.errstring = msg;
                       return val;
                   }
                    return val;
                });               
            }
        }

0 个答案:

没有答案