diff --git a/src/EditorFeatures/Core/Implementation/ForegroundNotification/ForegroundNotificationService.cs b/src/EditorFeatures/Core/Implementation/ForegroundNotification/ForegroundNotificationService.cs index 348613ca16dff5b5d27f75a01378966219bde90c..a2b92c6f1fd8f8331cc0ce18c9621816b6931479 100644 --- a/src/EditorFeatures/Core/Implementation/ForegroundNotification/ForegroundNotificationService.cs +++ b/src/EditorFeatures/Core/Implementation/ForegroundNotification/ForegroundNotificationService.cs @@ -196,26 +196,29 @@ private void NotifyOnForegroundWorker() private async Task WaitForPendingWorkAsync() { - await _workQueue.WaitForItemsAsync().ConfigureAwait(false); while (true) { - var current = Environment.TickCount; - var nextItem = _workQueue.PeekNextItemTime(); + await _workQueue.WaitForItemsAsync().ConfigureAwait(false); + if (!_workQueue.TryPeekNextItemTime(out var nextItem)) + { + // Need to go back and wait for an item + continue; + } // The next item is ready to run - if (nextItem - current <= 0) + if (nextItem - Environment.TickCount <= 0) { break; } // wait some and re-check since there could be another one inserted before the first one while we were waiting. - await Task.Delay(MinimumDelayBetweenProcessing).ConfigureAwait(continueOnCapturedContext: false); + await Task.Delay(MinimumDelayBetweenProcessing).ConfigureAwait(false); } // Throttle how often we run by waiting MinimumDelayBetweenProcessing since the last time we processed notifications if (Environment.TickCount - _lastProcessedTimeInMS < MinimumDelayBetweenProcessing) { - await Task.Delay(MinimumDelayBetweenProcessing).ConfigureAwait(continueOnCapturedContext: false); + await Task.Delay(MinimumDelayBetweenProcessing).ConfigureAwait(false); } } @@ -344,12 +347,18 @@ private void Enqueue_NoLock(LinkedListNode entry) return; } - public int PeekNextItemTime() + public bool TryPeekNextItemTime(out int minimumRunPoint) { lock (_gate) { - Contract.Requires(_list.Count > 0); - return _list.First.Value.MinimumRunPointInMS; + if (_list.Count == 0) + { + minimumRunPoint = 0; + return false; + } + + minimumRunPoint = _list.First.Value.MinimumRunPointInMS; + return true; } }