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

Commit e9c8db9

Browse files
committed
Revert "Allow menuService.AddCommands to be called from a b/g thread"
This reverts commit 0527497.
1 parent 47d3753 commit e9c8db9

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

src/GitHub.InlineReviews/InlineReviewsPackage.cs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using GitHub.VisualStudio;
99
using Microsoft.VisualStudio.ComponentModelHost;
1010
using Microsoft.VisualStudio.Shell;
11+
using Microsoft.VisualStudio.Threading;
1112
using Task = System.Threading.Tasks.Task;
1213

1314
namespace GitHub.InlineReviews
@@ -27,15 +28,24 @@ protected override async Task InitializeAsync(
2728
var componentModel = (IComponentModel)(await GetServiceAsync(typeof(SComponentModel)));
2829
var exports = componentModel.DefaultExportProvider;
2930

30-
menuService.AddCommands(
31-
exports.GetExportedValue<INextInlineCommentCommand>(),
32-
exports.GetExportedValue<IPreviousInlineCommentCommand>());
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);
3334
}
3435

35-
// The IDesignerHost and ISelectionService services are requested by MenuCommandService.EnsureVerbs().
36-
// When called from a non-Main thread this would throw despite the fact these services don't exist.
37-
// This override allows IMenuCommandService.AddCommands to be called form a background thread.
38-
protected override object GetService(Type serviceType)
39-
=> (serviceType == typeof(ISelectionService) || serviceType == typeof(IDesignerHost)) ? null : base.GetService(serviceType);
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+
var commands = new IVsCommandBase[]
42+
{
43+
exports.GetExportedValue<INextInlineCommentCommand>(),
44+
exports.GetExportedValue<IPreviousInlineCommentCommand>()
45+
};
46+
47+
await JoinableTaskFactory.SwitchToMainThreadAsync();
48+
menuService.AddCommands(commands);
49+
}
4050
}
4151
}

src/GitHub.VisualStudio/GitHubPackage.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,11 @@ protected override async Task InitializeAsync(CancellationToken cancellationToke
3838
LogVersionInformation();
3939
await base.InitializeAsync(cancellationToken, progress);
4040
await GetServiceAsync(typeof(IUsageTracker));
41-
await InitializeMenus();
42-
}
4341

44-
// The IDesignerHost and ISelectionService services are requested by MenuCommandService.EnsureVerbs().
45-
// When called from a non-Main thread this would throw despite the fact these services don't exist.
46-
// This override allows IMenuCommandService.AddCommands to be called form a background thread.
47-
protected override object GetService(Type serviceType)
48-
=> (serviceType == typeof(ISelectionService) || serviceType == typeof(IDesignerHost)) ? null : base.GetService(serviceType);
42+
// Avoid delays when there is ongoing UI activity.
43+
// See: https://github.com/github/VisualStudio/issues/1537
44+
await JoinableTaskFactory.RunAsync(VsTaskRunContext.UIThreadNormalPriority, InitializeMenus);
45+
}
4946

5047
void LogVersionInformation()
5148
{
@@ -60,16 +57,20 @@ async Task InitializeMenus()
6057
var menuService = (IMenuCommandService)(await GetServiceAsync(typeof(IMenuCommandService)));
6158
var componentModel = (IComponentModel)(await GetServiceAsync(typeof(SComponentModel)));
6259
var exports = componentModel.DefaultExportProvider;
63-
64-
menuService.AddCommands(
60+
var commands = new IVsCommandBase[]
61+
{
6562
exports.GetExportedValue<IAddConnectionCommand>(),
6663
exports.GetExportedValue<IBlameLinkCommand>(),
6764
exports.GetExportedValue<ICopyLinkCommand>(),
6865
exports.GetExportedValue<ICreateGistCommand>(),
6966
exports.GetExportedValue<IOpenLinkCommand>(),
7067
exports.GetExportedValue<IOpenPullRequestsCommand>(),
7168
exports.GetExportedValue<IShowCurrentPullRequestCommand>(),
72-
exports.GetExportedValue<IShowGitHubPaneCommand>());
69+
exports.GetExportedValue<IShowGitHubPaneCommand>()
70+
};
71+
72+
await JoinableTaskFactory.SwitchToMainThreadAsync();
73+
menuService.AddCommands(commands);
7374
}
7475

7576
async Task EnsurePackageLoaded(Guid packageGuid)

0 commit comments

Comments
 (0)