Skip to content

Commit

Permalink
Use vs-threading extensions where available
Browse files Browse the repository at this point in the history
* Avoid throwing cancellation exceptions (follow-up to dotnet#68412)
* Use provided NoThrowAwaitable extension method
  • Loading branch information
sharwell committed Sep 13, 2023
1 parent 0959e07 commit 3cf1090
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 145 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,11 @@ private ValueTask<VoidResult> ProcessEventChangeAsync(ImmutableSegmentedList<boo
/// </param>
private async Task<VoidResult> RecomputeTagsAsync(bool highPriority, CancellationToken cancellationToken)
{
await _dataSource.ThreadingContext.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);
#pragma warning disable VSTHRD004 // Await SwitchToMainThreadAsync
await _dataSource.ThreadingContext.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken).NoThrowAwaitable();
#pragma warning restore VSTHRD004 // Await SwitchToMainThreadAsync
if (cancellationToken.IsCancellationRequested)
return default;

// if we're tagging documents that are not visible, then introduce a long delay so that we avoid
// consuming machine resources on work the user isn't likely to see. ConfigureAwait(true) so that if
Expand Down Expand Up @@ -247,7 +251,11 @@ await _visibilityTracker.DelayWhileNonVisibleAsync(
var bufferToChanges = ProcessNewTagTrees(spansToTag, oldTagTrees, newTagTrees, cancellationToken);

// Then switch back to the UI thread to update our state and kick off the work to notify the editor.
await _dataSource.ThreadingContext.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);
#pragma warning disable VSTHRD004 // Await SwitchToMainThreadAsync
await _dataSource.ThreadingContext.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken).NoThrowAwaitable();
#pragma warning restore VSTHRD004 // Await SwitchToMainThreadAsync
if (cancellationToken.IsCancellationRequested)
return default;

// Once we assign our state, we're uncancellable. We must report the changed information
// to the editor. The only case where it's ok not to is if the tagger itself is disposed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,12 @@ async Task DelayWhileNonVisibleWorkerAsync()
if (cancellationToken.IsCancellationRequested)
return;

await threadingContext.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);
#pragma warning disable VSTHRD004 // Await SwitchToMainThreadAsync
await threadingContext.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken).NoThrowAwaitable();
#pragma warning restore VSTHRD004 // Await SwitchToMainThreadAsync
if (cancellationToken.IsCancellationRequested)
return;

if (service.IsVisible(subjectBuffer))
return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ private async Task ProcessQueueAsync()
}

// wait for all pending tasks to complete their cancellation, ignoring any exceptions
await Task.WhenAll(concurrentlyExecutingTasksArray.Select(kvp => kvp.Key)).NoThrowAwaitableInternal(captureContext: false);
await Task.WhenAll(concurrentlyExecutingTasksArray.Select(kvp => kvp.Key)).NoThrowAwaitable(captureContext: false);
}

Debug.Assert(!concurrentlyExecutingTasks.Any(), "The tasks should have all been drained before continuing");
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Shared.TestHooks;
using Microsoft.CodeAnalysis.Telemetry;
using Microsoft.CodeAnalysis.Text;
using Microsoft.CodeAnalysis.Workspaces.Diagnostics;
using Microsoft.VisualStudio.Threading;
using Roslyn.Utilities;
using static Microsoft.VisualStudio.Threading.ThreadingTools;

Expand Down Expand Up @@ -307,11 +307,11 @@ static async Task WaitForHighPriorityTasksAsync(CancellationToken cancellationTo
if (task.IsCompleted)
{
// Make sure to yield so continuations of 'task' can make progress.
await Task.Yield().ConfigureAwait(false);
await AwaitExtensions.ConfigureAwait(Task.Yield(), false);
}
else
{
await task.WithCancellation(cancellationToken).NoThrowAwaitableInternal(false);
await task.WithCancellation(cancellationToken).NoThrowAwaitable(false);
}
}
}
Expand Down

0 comments on commit 3cf1090

Please sign in to comment.