Skip to content

Commit ba524a7

Browse files
committed
Switch to vs-threading pattern for UI-thread affinitized code
The implementations of ISuggestedAction use threading assertions instead of automatic transition to the correct thread, placing the burden of correct thread selection on the caller, TextViewWindow_InProc. This change switches to the vs-threading approach for maintaining thread affinity, which has two parts: 1. When entering thread-affinitized code, switch to the correct thread 2. Avoid using ConfigureAwait(false) to lose the required affinity The entire file relies on the default behavior of ConfigureAwait, which is the normal coding practice in asynchronous code following vs-threading patterns.
1 parent aefa753 commit ba524a7

1 file changed

Lines changed: 20 additions & 10 deletions

File tree

src/VisualStudio/IntegrationTest/TestUtilities/InProcess/TextViewWindow_InProc.cs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public void WaitForLightBulbSession()
8484

8585
var view = GetActiveTextView();
8686
var broker = GetComponentModel().GetService<ILightBulbBroker>();
87-
await LightBulbHelper.WaitForLightBulbSessionAsync(broker, view).ConfigureAwait(false);
87+
await LightBulbHelper.WaitForLightBulbSessionAsync(broker, view);
8888
});
8989
}
9090

@@ -288,12 +288,14 @@ public string[] GetLightBulbActions()
288288

289289
var view = GetActiveTextView();
290290
var broker = GetComponentModel().GetService<ILightBulbBroker>();
291-
return (await GetLightBulbActionsAsync(broker, view).ConfigureAwait(false)).Select(a => a.DisplayText).ToArray();
291+
return (await GetLightBulbActionsAsync(broker, view)).Select(a => a.DisplayText).ToArray();
292292
});
293293
}
294294

295295
private async Task<IEnumerable<ISuggestedAction>> GetLightBulbActionsAsync(ILightBulbBroker broker, IWpfTextView view)
296296
{
297+
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
298+
297299
if (!broker.IsLightBulbSessionActive(view))
298300
{
299301
var bufferType = view.TextBuffer.ContentType.DisplayName;
@@ -312,7 +314,7 @@ private async Task<IEnumerable<ISuggestedAction>> GetLightBulbActionsAsync(ILigh
312314
actionSets = Array.Empty<SuggestedActionSet>();
313315
}
314316

315-
return await SelectActionsAsync(actionSets).ConfigureAwait(false);
317+
return await SelectActionsAsync(actionSets);
316318
}
317319

318320
public void ApplyLightBulbAction(string actionName, FixAllScope? fixAllScope, bool blockUntilComplete)
@@ -323,7 +325,7 @@ public void ApplyLightBulbAction(string actionName, FixAllScope? fixAllScope, bo
323325
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
324326

325327
var activeTextView = GetActiveTextView();
326-
await lightBulbAction(activeTextView).ConfigureAwait(false);
328+
await lightBulbAction(activeTextView);
327329
});
328330

329331
if (blockUntilComplete)
@@ -342,9 +344,11 @@ private Func<IWpfTextView, Task> GetLightBulbApplicationAction(string actionName
342344
{
343345
return async view =>
344346
{
347+
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
348+
345349
var broker = GetComponentModel().GetService<ILightBulbBroker>();
346350

347-
var actions = (await GetLightBulbActionsAsync(broker, view).ConfigureAwait(true)).ToArray();
351+
var actions = (await GetLightBulbActionsAsync(broker, view)).ToArray();
348352
var action = actions.FirstOrDefault(a => a.DisplayText == actionName);
349353

350354
if (action == null)
@@ -367,8 +371,8 @@ private Func<IWpfTextView, Task> GetLightBulbApplicationAction(string actionName
367371
throw new InvalidOperationException($"Suggested action '{action.DisplayText}' does not support FixAllOccurrences.");
368372
}
369373

370-
var actionSetsForAction = await action.GetActionSetsAsync(CancellationToken.None).ConfigureAwait(true);
371-
action = await GetFixAllSuggestedActionAsync(actionSetsForAction, fixAllScope.Value).ConfigureAwait(true);
374+
var actionSetsForAction = await action.GetActionSetsAsync(CancellationToken.None);
375+
action = await GetFixAllSuggestedActionAsync(actionSetsForAction, fixAllScope.Value);
372376
if (action == null)
373377
{
374378
throw new InvalidOperationException($"Unable to find FixAll in {fixAllScope.ToString()} code fix for suggested action '{action.DisplayText}'.");
@@ -389,6 +393,8 @@ private Func<IWpfTextView, Task> GetLightBulbApplicationAction(string actionName
389393

390394
private async Task<IEnumerable<ISuggestedAction>> SelectActionsAsync(IEnumerable<SuggestedActionSet> actionSets)
391395
{
396+
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
397+
392398
var actions = new List<ISuggestedAction>();
393399

394400
if (actionSets != null)
@@ -400,7 +406,9 @@ private async Task<IEnumerable<ISuggestedAction>> SelectActionsAsync(IEnumerable
400406
foreach (var action in actionSet.Actions)
401407
{
402408
actions.Add(action);
403-
actions.AddRange(await SelectActionsAsync(await action.GetActionSetsAsync(CancellationToken.None).ConfigureAwait(false)).ConfigureAwait(false));
409+
var nestedActionSets = await action.GetActionSetsAsync(CancellationToken.None);
410+
var nestedActions = await SelectActionsAsync(nestedActionSets);
411+
actions.AddRange(nestedActions);
404412
}
405413
}
406414
}
@@ -411,6 +419,8 @@ private async Task<IEnumerable<ISuggestedAction>> SelectActionsAsync(IEnumerable
411419

412420
private static async Task<FixAllSuggestedAction> GetFixAllSuggestedActionAsync(IEnumerable<SuggestedActionSet> actionSets, FixAllScope fixAllScope)
413421
{
422+
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
423+
414424
foreach (var actionSet in actionSets)
415425
{
416426
foreach (var action in actionSet.Actions)
@@ -426,8 +436,8 @@ private static async Task<FixAllSuggestedAction> GetFixAllSuggestedActionAsync(I
426436

427437
if (action.HasActionSets)
428438
{
429-
var nestedActionSets = await action.GetActionSetsAsync(CancellationToken.None).ConfigureAwait(false);
430-
fixAllSuggestedAction = await GetFixAllSuggestedActionAsync(nestedActionSets, fixAllScope).ConfigureAwait(false);
439+
var nestedActionSets = await action.GetActionSetsAsync(CancellationToken.None);
440+
fixAllSuggestedAction = await GetFixAllSuggestedActionAsync(nestedActionSets, fixAllScope);
431441
if (fixAllSuggestedAction != null)
432442
{
433443
return fixAllSuggestedAction;

0 commit comments

Comments
 (0)