等待Task.Delay()延迟预期的时间

我有一个Windows服务应用程序,它大量使用.Net任务。 我对他们有一个很好的理解,但我不是亲,我遇到了问题。

我有一种情况,我的await Task.Delay()有时需要更长的时间(最多60秒)。 我确信这是由于我正在做的事情,但是我无法追踪它可能会影响到这一点。

这是我正在做的事情类型的一个简化的人为例子。

 public async Task DoDelay(CancellationToken token = default(CancellationToken)) { // Delay for then check to see if we need to extend our visibility timeout DateTime beforeDelay = DateTime.Now; // Wait for 180 seconds, ie 3 minutes await Task.Delay((int)180 * 1000, token).ConfigureAwait(false); // Check to see how long we actually delayed for TimeSpan durationOfDelay = DateTime.Now - beforeDelay; Trace.WriteLine("Duration: " + durationOfDelay.TotalSeconds.ToString()); } 

有时, durationOfDelay比我延迟的durationOfDelay长达60秒。

作为一些背景,可能有多个其他任务调用DoDelay()并同时运行。 我有时看到,所有期待的Task.Delay()调用都在同一时间完成,尽管在不同时间等待具有相同的延迟值。

例如:

 // Non-awaited call executed at 00:00 DoDelay(); // Non-awaited call executed at 00:10 DoDelay(); // Non-awaited call executed at 00:20 DoDelay(); // Time goes by.... // Trace output: // "Duration: 200" // "Duration: 190" // "Duration: 180" 

什么会导致这类事情?