Skip to content

Commit 58a1749

Browse files
committed
Cancel inspections.
1 parent 2b3f368 commit 58a1749

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-10
lines changed

RetailCoder.VBE/UI/Inspections/InspectionResultsViewModel.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ private void _configService_SettingsChanged(object sender, ConfigurationChangedE
102102
{
103103
if (e.InspectionSettingsChanged)
104104
{
105-
RefreshInspections();
105+
RefreshInspections(CancellationToken.None);
106106
}
107107
_runInspectionsOnReparse = e.RunInspectionsOnReparse;
108108
}
@@ -269,7 +269,7 @@ public bool IsBusy
269269
}
270270

271271
private bool _runInspectionsOnReparse;
272-
private void HandleStateChanged(object sender, EventArgs e)
272+
private void HandleStateChanged(object sender, ParserStateEventArgs e)
273273
{
274274
if(!IsRefreshing && (_state.Status == ParserState.Pending || _state.Status == ParserState.Error || _state.Status == ParserState.ResolverError))
275275
{
@@ -284,16 +284,27 @@ private void HandleStateChanged(object sender, EventArgs e)
284284

285285
if (_runInspectionsOnReparse || IsRefreshing)
286286
{
287-
RefreshInspections();
287+
RefreshInspections(e.Token);
288288
}
289289
}
290290

291-
private async void RefreshInspections()
291+
private async void RefreshInspections(CancellationToken token)
292292
{
293293
var stopwatch = Stopwatch.StartNew();
294294
IsBusy = true;
295295

296-
var results = (await _inspector.FindIssuesAsync(_state, CancellationToken.None)).ToList();
296+
List<IInspectionResult> results;
297+
try
298+
{
299+
var inspectionResults = await _inspector.FindIssuesAsync(_state, token);
300+
results = inspectionResults.ToList();
301+
}
302+
catch (OperationCanceledException)
303+
{
304+
Logger.Debug("Inspections got canceled.");
305+
return; //We throw away the partial results.
306+
}
307+
297308
if (GroupByInspectionType)
298309
{
299310
results = results.OrderBy(o => o.Inspection.InspectionType)

Rubberduck.Inspections/Inspector.cs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,23 +61,27 @@ public async Task<IEnumerable<IInspectionResult>> FindIssuesAsync(RubberduckPars
6161
{
6262
return new IInspectionResult[] { };
6363
}
64+
token.ThrowIfCancellationRequested();
6465

6566
state.OnStatusMessageUpdate(RubberduckUI.CodeInspections_Inspecting);
6667
var allIssues = new ConcurrentBag<IInspectionResult>();
68+
token.ThrowIfCancellationRequested();
6769

6870
var config = _configService.LoadConfiguration();
6971
UpdateInspectionSeverity(config);
72+
token.ThrowIfCancellationRequested();
7073

7174
var parseTreeInspections = _inspections
7275
.Where(inspection => inspection.Severity != CodeInspectionSeverity.DoNotShow)
7376
.OfType<IParseTreeInspection>()
7477
.ToArray();
78+
token.ThrowIfCancellationRequested();
7579

76-
foreach(var listener in parseTreeInspections.Select(inspection => inspection.Listener))
80+
foreach (var listener in parseTreeInspections.Select(inspection => inspection.Listener))
7781
{
7882
listener.ClearContexts();
7983
}
80-
84+
8185
// Prepare ParseTreeWalker based inspections
8286
var passes = Enum.GetValues(typeof (ParsePass)).Cast<ParsePass>();
8387
foreach (var parsePass in passes)
@@ -91,8 +95,10 @@ public async Task<IEnumerable<IInspectionResult>> FindIssuesAsync(RubberduckPars
9195
LogManager.GetCurrentClassLogger().Warn(e);
9296
}
9397
}
98+
token.ThrowIfCancellationRequested();
9499

95100
var inspectionsToRun = _inspections.Where(inspection => inspection.Severity != CodeInspectionSeverity.DoNotShow);
101+
token.ThrowIfCancellationRequested();
96102

97103
try
98104
{
@@ -102,13 +108,18 @@ public async Task<IEnumerable<IInspectionResult>> FindIssuesAsync(RubberduckPars
102108
{
103109
if (exception.Flatten().InnerExceptions.All(ex => ex is OperationCanceledException))
104110
{
105-
LogManager.GetCurrentClassLogger().Debug("Inspections got canceled.");
111+
//This eliminates the stack trace, but for the cancellation, this is irrelevant.
112+
throw exception.InnerException ?? exception;
106113
}
107114
else
108115
{
109116
LogManager.GetCurrentClassLogger().Error(exception);
110117
}
111118
}
119+
catch (OperationCanceledException)
120+
{
121+
throw;
122+
}
112123
catch (Exception e)
113124
{
114125
LogManager.GetCurrentClassLogger().Error(e);
@@ -137,21 +148,27 @@ private static void RunInspectionsInParallel(IEnumerable<IInspection> inspection
137148

138149
Parallel.ForEach(inspectionsToRun,
139150
options,
140-
inspection => RunInspection(inspection, allIssues)
151+
inspection => RunInspection(inspection, allIssues, token)
141152
);
142153
}
143154

144-
private static void RunInspection(IInspection inspection, ConcurrentBag<IInspectionResult> allIssues)
155+
private static void RunInspection(IInspection inspection, ConcurrentBag<IInspectionResult> allIssues, CancellationToken token)
145156
{
146157
try
147158
{
148159
var inspectionResults = inspection.GetInspectionResults();
149160

161+
token.ThrowIfCancellationRequested();
162+
150163
foreach (var inspectionResult in inspectionResults)
151164
{
152165
allIssues.Add(inspectionResult);
153166
}
154167
}
168+
catch (OperationCanceledException)
169+
{
170+
throw;
171+
}
155172
catch (Exception e)
156173
{
157174
LogManager.GetCurrentClassLogger().Warn(e);

0 commit comments

Comments
 (0)