Skip to content

Commit 134e862

Browse files
committed
Update workspace event registration
1 parent c17c926 commit 134e862

File tree

4 files changed

+12
-16
lines changed

4 files changed

+12
-16
lines changed

src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/Discovery/ProjectStateChangeDetector.TestAccessor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public Task ListenForWorkspaceChangesAsync(params WorkspaceChangeKind[] kinds)
3939

4040
public void WorkspaceChanged(WorkspaceChangeEventArgs e)
4141
{
42-
instance.Workspace_WorkspaceChanged(instance, e);
42+
instance.Workspace_WorkspaceChanged(e);
4343
}
4444
}
4545
}

src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/Discovery/ProjectStateChangeDetector.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public override int GetHashCode()
3737
private readonly ProjectSnapshotManager _projectManager;
3838
private readonly LanguageServerFeatureOptions _options;
3939
private readonly CodeAnalysis.Workspace _workspace;
40-
40+
private readonly WorkspaceEventRegistration _changeRegistration;
4141
private readonly CancellationTokenSource _disposeTokenSource;
4242
private readonly AsyncBatchingWorkQueue<Work> _workQueue;
4343
private readonly HashSet<Work> _workerSet;
@@ -80,7 +80,7 @@ public ProjectStateChangeDetector(
8080
_projectManager.Changed += ProjectManager_Changed;
8181

8282
_workspace = workspaceProvider.GetWorkspace();
83-
_workspace.WorkspaceChanged += Workspace_WorkspaceChanged;
83+
_changeRegistration = _workspace.RegisterWorkspaceChangedHandler(Workspace_WorkspaceChanged);
8484

8585
// This will usually no-op, in the case that another project snapshot change trigger
8686
// immediately adds projects we want to be able to handle those projects.
@@ -95,7 +95,7 @@ public void Dispose()
9595
}
9696

9797
_projectManager.Changed -= ProjectManager_Changed;
98-
_workspace.WorkspaceChanged -= Workspace_WorkspaceChanged;
98+
_changeRegistration.Dispose();
9999

100100
_disposeTokenSource.Cancel();
101101
_disposeTokenSource.Dispose();
@@ -118,7 +118,7 @@ private ValueTask ProcessBatchAsync(ImmutableArray<Work> items, CancellationToke
118118
return default;
119119
}
120120

121-
private void Workspace_WorkspaceChanged(object? sender, WorkspaceChangeEventArgs e)
121+
private void Workspace_WorkspaceChanged(WorkspaceChangeEventArgs e)
122122
{
123123
switch (e.Kind)
124124
{

src/Razor/src/Microsoft.VisualStudioCode.RazorExtension/RazorWorkspaceListenerBase.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Collections.Immutable;
7-
using System.Diagnostics;
87
using System.IO;
98
using System.Threading;
109
using System.Threading.Tasks;
@@ -29,6 +28,7 @@ internal abstract class RazorWorkspaceListenerBase : IDisposable
2928

3029
private Stream? _stream;
3130
private Workspace? _workspace;
31+
private WorkspaceEventRegistration? _changeRegistration;
3232
private bool _disposed;
3333

3434
internal record Work(ProjectId ProjectId);
@@ -45,10 +45,7 @@ protected RazorWorkspaceListenerBase(ILogger logger)
4545

4646
public void Dispose()
4747
{
48-
if (_workspace is not null)
49-
{
50-
_workspace.WorkspaceChanged -= Workspace_WorkspaceChanged;
51-
}
48+
_changeRegistration?.Dispose();
5249

5350
if (_disposed)
5451
{
@@ -57,7 +54,7 @@ public void Dispose()
5754
}
5855

5956
_disposed = true;
60-
_logger.LogInformation("Tearing down named pipe for pid {pid}", Process.GetCurrentProcess().Id);
57+
_logger.LogInformation("Tearing down named pipe for pid {pid}", Environment.ProcessId);
6158

6259
_disposeTokenSource.Cancel();
6360
_disposeTokenSource.Dispose();
@@ -105,11 +102,11 @@ private protected void EnsureInitialized(Workspace workspace, Func<Stream> creat
105102
}
106103

107104
_workspace = workspace;
108-
_workspace.WorkspaceChanged += Workspace_WorkspaceChanged;
105+
_changeRegistration = _workspace.RegisterWorkspaceChangedHandler(Workspace_WorkspaceChanged);
109106
_stream = createStream();
110107
}
111108

112-
private void Workspace_WorkspaceChanged(object? sender, WorkspaceChangeEventArgs e)
109+
private void Workspace_WorkspaceChanged(WorkspaceChangeEventArgs e)
113110
{
114111
switch (e.Kind)
115112
{

src/Razor/test/Microsoft.AspNetCore.Razor.Test.Common.Tooling/Workspaces/WorkspaceTestBase.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ protected Task<bool> UpdateWorkspaceAsync(Solution solution)
126126
{
127127
var currentCount = 0;
128128

129-
Workspace.WorkspaceChanged += OnWorkspaceChanged;
129+
using var _ = Workspace.RegisterWorkspaceChangedHandler(OnWorkspaceChanged);
130130

131131
if (!Workspace.TryApplyChanges(solution))
132132
{
@@ -142,10 +142,9 @@ protected Task<bool> UpdateWorkspaceAsync(Solution solution)
142142
}
143143
while (lastCount != currentCount);
144144

145-
Workspace.WorkspaceChanged -= OnWorkspaceChanged;
146145
return true;
147146

148-
void OnWorkspaceChanged(object? sender, WorkspaceChangeEventArgs e)
147+
void OnWorkspaceChanged(WorkspaceChangeEventArgs e)
149148
{
150149
currentCount++;
151150
}

0 commit comments

Comments
 (0)