diff --git a/src/EditorFeatures/Core/Implementation/ForegroundNotification/ForegroundNotificationService.cs b/src/EditorFeatures/Core/Implementation/ForegroundNotification/ForegroundNotificationService.cs index 1aed8a195646861bc4b8196879e005f41ee6d278..348613ca16dff5b5d27f75a01378966219bde90c 100644 --- a/src/EditorFeatures/Core/Implementation/ForegroundNotification/ForegroundNotificationService.cs +++ b/src/EditorFeatures/Core/Implementation/ForegroundNotification/ForegroundNotificationService.cs @@ -12,6 +12,7 @@ using Microsoft.CodeAnalysis.Shared.TestHooks; using Roslyn.Utilities; using Microsoft.CodeAnalysis.ErrorReporting; +using Microsoft.CodeAnalysis.Utilities; namespace Microsoft.CodeAnalysis.Editor.Implementation.ForegroundNotification { @@ -35,7 +36,11 @@ public ForegroundNotificationService() _workQueue = new PriorityQueue(); _lastProcessedTimeInMS = Environment.TickCount; - Task.Factory.SafeStartNewFromAsync(ProcessAsync, CancellationToken.None, TaskScheduler.Default); + // Only start the background processing task if foreground work is allowed + if (ForegroundKind != ForegroundThreadDataKind.Unknown) + { + Task.Factory.SafeStartNewFromAsync(ProcessAsync, CancellationToken.None, TaskScheduler.Default); + } } public void RegisterNotification(Action action, IAsyncToken asyncToken, CancellationToken cancellationToken = default) @@ -58,6 +63,9 @@ public void RegisterNotification(Action action, int delay, IAsyncToken asyncToke return; } + // Assert we have some kind of foreground thread + Contract.ThrowIfTrue(CurrentForegroundThreadData.Kind == ForegroundThreadDataKind.Unknown); + var current = Environment.TickCount; _workQueue.Enqueue(new PendingWork(current + delay, action, asyncToken, cancellationToken)); @@ -73,6 +81,9 @@ public void RegisterNotification(Func action, int delay, IAsyncToken async return; } + // Assert we have some kind of foreground thread + Contract.ThrowIfTrue(CurrentForegroundThreadData.Kind == ForegroundThreadDataKind.Unknown); + var current = Environment.TickCount; _workQueue.Enqueue(new PendingWork(current + delay, action, asyncToken, cancellationToken));