Skip to content

Commit fb244c7

Browse files
Merge pull request #74744 from CyrusNajmabadi/avoidAsyncVoid
2 parents e261c18 + 0970498 commit fb244c7

File tree

1 file changed

+32
-25
lines changed

1 file changed

+32
-25
lines changed

src/Workspaces/Core/Portable/Workspace/ProjectSystem/ProjectSystemProjectFactory.cs

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -785,44 +785,51 @@ public static PortableExecutableReference CreateMetadataReference_NoLock(string
785785
return reference;
786786
}
787787

788-
#pragma warning disable VSTHRD100 // Avoid async void methods
789-
private async void StartRefreshingMetadataReferencesForFile(object? sender, string fullFilePath)
790-
#pragma warning restore VSTHRD100 // Avoid async void methods
788+
private void StartRefreshingMetadataReferencesForFile(object? sender, string fullFilePath)
791789
{
792-
using var asyncToken = WorkspaceListener.BeginAsyncOperation(nameof(StartRefreshingMetadataReferencesForFile));
790+
var asyncToken = WorkspaceListener.BeginAsyncOperation(nameof(StartRefreshingMetadataReferencesForFile));
793791

794-
await ApplyBatchChangeToWorkspaceAsync((solutionChanges, projectUpdateState) =>
792+
var task = StartRefreshingMetadataReferencesForFileAsync(sender, fullFilePath);
793+
task.CompletesAsyncOperation(asyncToken);
794+
795+
return;
796+
797+
async Task StartRefreshingMetadataReferencesForFileAsync(object? sender, string fullFilePath)
795798
{
796-
// Access the current update state under the workspace sync.
797-
foreach (var project in Workspace.CurrentSolution.Projects)
799+
await ApplyBatchChangeToWorkspaceAsync((solutionChanges, projectUpdateState) =>
798800
{
799-
// Loop to find each reference with the given path. It's possible that there might be multiple references of the same path;
800-
// the project system could concievably add the same reference multiple times but with different aliases. It's also possible
801-
// we might not find the path at all: when we receive the file changed event, we aren't checking if the file is still
802-
// in the workspace at that time; it's possible it might have already been removed.
803-
foreach (var portableExecutableReference in project.MetadataReferences.OfType<PortableExecutableReference>())
801+
// Access the current update state under the workspace sync.
802+
foreach (var project in Workspace.CurrentSolution.Projects)
804803
{
805-
if (portableExecutableReference.FilePath == fullFilePath)
804+
// Loop to find each reference with the given path. It's possible that there might be multiple references of the same path;
805+
// the project system could concievably add the same reference multiple times but with different aliases. It's also possible
806+
// we might not find the path at all: when we receive the file changed event, we aren't checking if the file is still
807+
// in the workspace at that time; it's possible it might have already been removed.
808+
foreach (var portableExecutableReference in project.MetadataReferences.OfType<PortableExecutableReference>())
806809
{
807-
projectUpdateState = projectUpdateState.WithIncrementalMetadataReferenceRemoved(portableExecutableReference);
810+
if (portableExecutableReference.FilePath == fullFilePath)
811+
{
812+
projectUpdateState = projectUpdateState.WithIncrementalMetadataReferenceRemoved(portableExecutableReference);
808813

809-
var newPortableExecutableReference = CreateMetadataReference_NoLock(
810-
portableExecutableReference.FilePath,
811-
portableExecutableReference.Properties,
812-
SolutionServices);
814+
var newPortableExecutableReference = CreateMetadataReference_NoLock(
815+
portableExecutableReference.FilePath,
816+
portableExecutableReference.Properties,
817+
SolutionServices);
813818

814-
projectUpdateState = projectUpdateState.WithIncrementalMetadataReferenceAdded(newPortableExecutableReference);
819+
projectUpdateState = projectUpdateState.WithIncrementalMetadataReferenceAdded(newPortableExecutableReference);
815820

816-
var newSolution = solutionChanges.Solution.RemoveMetadataReference(project.Id, portableExecutableReference)
817-
.AddMetadataReference(project.Id, newPortableExecutableReference);
821+
var newSolution = solutionChanges.Solution
822+
.RemoveMetadataReference(project.Id, portableExecutableReference)
823+
.AddMetadataReference(project.Id, newPortableExecutableReference);
818824

819-
solutionChanges.UpdateSolutionForProjectAction(project.Id, newSolution);
825+
solutionChanges.UpdateSolutionForProjectAction(project.Id, newSolution);
826+
}
820827
}
821828
}
822-
}
823829

824-
return projectUpdateState;
825-
}, onAfterUpdateAlways: null).ConfigureAwait(false);
830+
return projectUpdateState;
831+
}, onAfterUpdateAlways: null).ConfigureAwait(false);
832+
}
826833
}
827834

828835
internal Task RaiseOnDocumentsAddedMaybeAsync(bool useAsync, ImmutableArray<string> filePaths)

0 commit comments

Comments
 (0)