相关疑难解决方法(0)

任务中的秒表似乎是所有任务的附加,想要测量任务间隔

我正在循环运行并以下列方式开始执行任务:

var iResult = new List<Task>();
foreach(var i in myCollection)
{
    var task = Task.Factory.StartNew(() =>
                    DoSomething(), TaskCreationOptions.LongRunning);
    task.ContinueWith(m => myResponseHandler(m.Result));
    iResult.Add(task);
}
Run Code Online (Sandbox Code Playgroud)

在我的DoSomething()方法中,我有一个计时器:

public static myMsg DoSomething()
{
    var timer = System.Diagnostics.Stopwatch.StartNew();
    DoLongRunningTask(); //If it matters this hits a REST endpoint (https)
    timer.Stop();

    return new myMsg(timer.ElaspedMilliseconds);
}
Run Code Online (Sandbox Code Playgroud)

当我遍历我的列表时,myMsgElaspedMilliseconds似乎完全是加法的 - 第一个上的ElaspedMilliseconds可能是300,但最后一个可能是50000(50秒) - 这实际上是整个事情所需的大致时间运行(由另一个计时器测量).

c# synchronization closures stopwatch task-parallel-library

6
推荐指数
1
解决办法
1774
查看次数