Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 5321090

Browse files
authored
Merge pull request #1538 from github/fixes/1537-SwitchToMainThreadAsync
Find workaround for SwitchToMainThreadAsync
2 parents 7f4cc25 + 4a1b5da commit 5321090

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

src/GitHub.InlineReviews/InlineReviewsPackage.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
using System.ComponentModel.Design;
33
using System.Runtime.InteropServices;
44
using System.Threading;
5-
using GitHub.Helpers;
65
using GitHub.Commands;
76
using GitHub.InlineReviews.Views;
87
using GitHub.Services.Vssdk.Commands;
98
using GitHub.VisualStudio;
109
using Microsoft.VisualStudio.ComponentModelHost;
1110
using Microsoft.VisualStudio.Shell;
11+
using Microsoft.VisualStudio.Threading;
1212
using Task = System.Threading.Tasks.Task;
1313

1414
namespace GitHub.InlineReviews
@@ -28,7 +28,18 @@ protected override async Task InitializeAsync(
2828
var componentModel = (IComponentModel)(await GetServiceAsync(typeof(SComponentModel)));
2929
var exports = componentModel.DefaultExportProvider;
3030

31-
await ThreadingHelper.SwitchToMainThreadAsync();
31+
// Avoid delays when there is ongoing UI activity.
32+
// See: https://github.com/github/VisualStudio/issues/1537
33+
await JoinableTaskFactory.RunAsync(VsTaskRunContext.UIThreadNormalPriority, InitializeMenus);
34+
}
35+
36+
async Task InitializeMenus()
37+
{
38+
var menuService = (IMenuCommandService)(await GetServiceAsync(typeof(IMenuCommandService)));
39+
var componentModel = (IComponentModel)(await GetServiceAsync(typeof(SComponentModel)));
40+
var exports = componentModel.DefaultExportProvider;
41+
42+
await JoinableTaskFactory.SwitchToMainThreadAsync();
3243
menuService.AddCommands(
3344
exports.GetExportedValue<INextInlineCommentCommand>(),
3445
exports.GetExportedValue<IPreviousInlineCommentCommand>());

src/GitHub.InlineReviews/PullRequestStatusBarPackage.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
using System;
22
using System.Threading;
33
using System.Runtime.InteropServices;
4-
using GitHub.Helpers;
54
using GitHub.Services;
65
using GitHub.VisualStudio;
76
using GitHub.InlineReviews.Services;
87
using Microsoft.VisualStudio.Shell;
98
using Task = System.Threading.Tasks.Task;
9+
using Microsoft.VisualStudio.Threading;
1010

1111
namespace GitHub.InlineReviews
1212
{
@@ -19,12 +19,19 @@ public class PullRequestStatusBarPackage : AsyncPackage
1919
/// Initialize the PR status UI on Visual Studio's status bar.
2020
/// </summary>
2121
protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
22+
{
23+
// Avoid delays when there is ongoing UI activity.
24+
// See: https://github.com/github/VisualStudio/issues/1537
25+
await JoinableTaskFactory.RunAsync(VsTaskRunContext.UIThreadNormalPriority, InitializeStatusBar);
26+
}
27+
28+
async Task InitializeStatusBar()
2229
{
2330
var usageTracker = (IUsageTracker)await GetServiceAsync(typeof(IUsageTracker));
2431
var serviceProvider = (IGitHubServiceProvider)await GetServiceAsync(typeof(IGitHubServiceProvider));
2532
var barManager = new PullRequestStatusBarManager(usageTracker, serviceProvider);
2633

27-
await ThreadingHelper.SwitchToMainThreadAsync();
34+
await JoinableTaskFactory.SwitchToMainThreadAsync();
2835
barManager.StartShowingStatus();
2936
}
3037
}

src/GitHub.VisualStudio/GitHubPackage.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ protected override async Task InitializeAsync(CancellationToken cancellationToke
4141
await base.InitializeAsync(cancellationToken, progress);
4242
await GetServiceAsync(typeof(IUsageTracker));
4343

44-
// This package might be loaded on demand so we must await initialization of menus.
45-
await InitializeMenus();
44+
// Avoid delays when there is ongoing UI activity.
45+
// See: https://github.com/github/VisualStudio/issues/1537
46+
await JoinableTaskFactory.RunAsync(VsTaskRunContext.UIThreadNormalPriority, InitializeMenus);
4647
}
4748

4849
void LogVersionInformation()
@@ -59,7 +60,7 @@ async Task InitializeMenus()
5960
var componentModel = (IComponentModel)(await GetServiceAsync(typeof(SComponentModel)));
6061
var exports = componentModel.DefaultExportProvider;
6162

62-
await ThreadingHelper.SwitchToMainThreadAsync();
63+
await JoinableTaskFactory.SwitchToMainThreadAsync();
6364
menuService.AddCommands(
6465
exports.GetExportedValue<IAddConnectionCommand>(),
6566
exports.GetExportedValue<IBlameLinkCommand>(),

0 commit comments

Comments
 (0)