【发布时间】:2018-07-11 09:04:25
【问题描述】:
我使用 continuationWith(anotherTask) 创建一个任务,如下所示。我想找出第一个任务完成它的工作所花费的时间。我在 task1 和子任务之间共享变量“task1StartedDateTime”。这会没有任何问题吗?
public static void MyMethod()
{
var task1StartedDateTime = DateTime.Now;
var task1 = doWorkAsync();
task1.ContinueWith(t1 => {
var task1TookTime = DateTime.Now - task1StartedDateTime;
Console.WriteLine($"Task 1 took {task1TookTime}");
//Some other work of this child task
});
}
【问题讨论】: