From 8ede7e472967279f703933763ca3d2f5aa8545ec Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Sat, 31 Mar 2018 17:08:26 -0500 Subject: [PATCH] Avoid scheduling work that's already cancelled While it doesn't make sense to still be requesting foreground actions after cancellation is requested, tracking down all callers of IForegroundNotificationService to ensure the proper checks are in place can be tedious. This change treats IForegroundNotificationService as the gatekeeper for scheduling asynchronous operations, and proactively cancels operations if requested before adding them to the work queue. --- .../ForegroundNotificationService.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/EditorFeatures/Core/Implementation/ForegroundNotification/ForegroundNotificationService.cs b/src/EditorFeatures/Core/Implementation/ForegroundNotification/ForegroundNotificationService.cs index 3cf7f628886..1aed8a19564 100644 --- a/src/EditorFeatures/Core/Implementation/ForegroundNotification/ForegroundNotificationService.cs +++ b/src/EditorFeatures/Core/Implementation/ForegroundNotification/ForegroundNotificationService.cs @@ -52,6 +52,12 @@ public void RegisterNotification(Action action, int delay, IAsyncToken asyncToke { Contract.Requires(delay >= 0); + if (cancellationToken.IsCancellationRequested) + { + asyncToken?.Dispose(); + return; + } + var current = Environment.TickCount; _workQueue.Enqueue(new PendingWork(current + delay, action, asyncToken, cancellationToken)); @@ -61,6 +67,12 @@ public void RegisterNotification(Func action, int delay, IAsyncToken async { Contract.Requires(delay >= 0); + if (cancellationToken.IsCancellationRequested) + { + asyncToken?.Dispose(); + return; + } + var current = Environment.TickCount; _workQueue.Enqueue(new PendingWork(current + delay, action, asyncToken, cancellationToken)); -- GitLab