我正在做一个
Task.Delay(durationInMilliseconds, cancellationToken).ContinueWith(taskResult =>{
if (!taskResult.IsCanceled && taskResult.IsCompleted)
{
//Do something
}
else if (taskResult.IsCanceled)
{
//persist the time spent before task got cancelled
}
});
如何跟踪任务在取消之前的持续时间?
答案 0 :(得分:1)
使用Stopwatch class。它包含一个高分辨率计时器。任务开始时启动计时器,当你想检查经过的时间时,如果是属性则调用一个。
Stopwatch stopwatch = Stopwatch.StartNew();
long elapsedMillis = stopwatch.ElapsedMilliseconds;