diff --git a/src/VisualStudio/Core/Def/Implementation/ProjectSystem/VisualStudioWorkspaceImpl.OpenFileTracker.cs b/src/VisualStudio/Core/Def/Implementation/ProjectSystem/VisualStudioWorkspaceImpl.OpenFileTracker.cs index 598edd492f288a9899c503c762556d0a7d474913..769161590273c2f4500d820f20ed5e33f960fc43 100644 --- a/src/VisualStudio/Core/Def/Implementation/ProjectSystem/VisualStudioWorkspaceImpl.OpenFileTracker.cs +++ b/src/VisualStudio/Core/Def/Implementation/ProjectSystem/VisualStudioWorkspaceImpl.OpenFileTracker.cs @@ -7,6 +7,7 @@ using System.Threading.Tasks; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.Editor.Shared.Utilities; +using Microsoft.CodeAnalysis.Shared.TestHooks; using Microsoft.CodeAnalysis.Text; using Microsoft.VisualStudio.ComponentModelHost; using Microsoft.VisualStudio.Editor; @@ -30,6 +31,7 @@ public sealed class OpenFileTracker private readonly VisualStudioWorkspaceImpl _workspace; private readonly IVsRunningDocumentTable4 _runningDocumentTable; private readonly IVsEditorAdaptersFactoryService _editorAdaptersFactoryService; + private readonly IAsynchronousOperationListener _asyncOperationListener; #region Fields read/written to from multiple threads to track files that need to be checked @@ -70,6 +72,7 @@ private OpenFileTracker(VisualStudioWorkspaceImpl workspace, IVsRunningDocumentT _foregroundAffinitization = new ForegroundThreadAffinitizedObject(workspace._threadingContext, assertIsForeground: true); _runningDocumentTable = runningDocumentTable; _editorAdaptersFactoryService = componentModel.GetService(); + _asyncOperationListener = componentModel.GetService().GetListener(FeatureAttribute.Workspace); } public async static Task CreateAsync(VisualStudioWorkspaceImpl workspace, IAsyncServiceProvider asyncServiceProvider) @@ -374,12 +377,14 @@ public void CheckForFilesBeingOpen(ImmutableArray newFileNames) if (shouldStartTask) { + var asyncToken = _asyncOperationListener.BeginAsyncOperation(nameof(CheckForFilesBeingOpen)); + Task.Run(async () => { await _foregroundAffinitization.ThreadingContext.JoinableTaskFactory.SwitchToMainThreadAsync(); CheckForFilesBeingOpenOnUIThread(); - }); + }).CompletesAsyncOperation(asyncToken); } }