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

Commit 7d4c45f

Browse files
committed
Avoid delays be initializing UI using UIThreadNormalPriority
This stops UI initialization happening on background priority when there is no other activity.
1 parent a9473d8 commit 7d4c45f

File tree

4 files changed

+32
-34
lines changed

4 files changed

+32
-34
lines changed

src/GitHub.Exports/Helpers/ThreadingHelper.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,6 @@ public static IAwaitable SwitchToPoolThreadAsync(TaskScheduler scheduler = null)
5959
new AwaitableWrapper(scheduler ?? TaskScheduler.Default);
6060
}
6161

62-
// HACK: This is a workaround because the following doesn't seem to work.
63-
//await JoinableTaskFactory
64-
// .WithPriority(VsTaskRunContext.UIThreadNormalPriority)
65-
// .SwitchToMainThreadAsync();
66-
public static Task RunOnMainThreadNormalPriority(Action action)
67-
{
68-
var service = (IVsTaskSchedulerService2)VsTaskLibraryHelper.ServiceInstance;
69-
var scheduler = service.GetTaskScheduler((uint)VsTaskRunContext.UIThreadNormalPriority);
70-
return Task.Factory.StartNew(action, default(CancellationToken), TaskCreationOptions.HideScheduler, scheduler);
71-
}
72-
7362
class AwaitableWrapper : IAwaitable
7463
{
7564
Func<IAwaiter> getAwaiter;

src/GitHub.InlineReviews/InlineReviewsPackage.cs

Lines changed: 10 additions & 5 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,10 +28,15 @@ protected override async Task InitializeAsync(
2828
var componentModel = (IComponentModel)(await GetServiceAsync(typeof(SComponentModel)));
2929
var exports = componentModel.DefaultExportProvider;
3030

31-
await ThreadingHelper.SwitchToMainThreadAsync();
32-
menuService.AddCommands(
33-
exports.GetExportedValue<INextInlineCommentCommand>(),
34-
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, async () =>
34+
{
35+
await JoinableTaskFactory.SwitchToMainThreadAsync();
36+
menuService.AddCommands(
37+
exports.GetExportedValue<INextInlineCommentCommand>(),
38+
exports.GetExportedValue<IPreviousInlineCommentCommand>());
39+
});
3540
}
3641
}
3742
}

src/GitHub.InlineReviews/PullRequestStatusBarPackage.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,13 @@ protected override async Task InitializeAsync(CancellationToken cancellationToke
2828
var serviceProvider = (IGitHubServiceProvider)await GetServiceAsync(typeof(IGitHubServiceProvider));
2929
var barManager = new PullRequestStatusBarManager(usageTracker, serviceProvider);
3030

31-
// Unfortunately this doesn't return until after the GitHub pane has finnished refreshing.
32-
//log.Information("SwitchToMainThreadAsync");
33-
//await JoinableTaskFactory
34-
// .WithPriority(VsTaskRunContext.UIThreadNormalPriority)
35-
// .SwitchToMainThreadAsync();
36-
37-
// Posting a task like this seems to work.
38-
await ThreadingHelper.RunOnMainThreadNormalPriority(() => barManager.StartShowingStatus());
31+
// Avoid delays when there is ongoing UI activity.
32+
// See: https://github.com/github/VisualStudio/issues/1537
33+
await JoinableTaskFactory.RunAsync(VsTaskRunContext.UIThreadNormalPriority, async () =>
34+
{
35+
await JoinableTaskFactory.SwitchToMainThreadAsync();
36+
barManager.StartShowingStatus();
37+
});
3938
}
4039

4140
}

src/GitHub.VisualStudio/GitHubPackage.cs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,21 @@ async Task InitializeMenus()
5959
var componentModel = (IComponentModel)(await GetServiceAsync(typeof(SComponentModel)));
6060
var exports = componentModel.DefaultExportProvider;
6161

62-
await ThreadingHelper.SwitchToMainThreadAsync();
63-
menuService.AddCommands(
64-
exports.GetExportedValue<IAddConnectionCommand>(),
65-
exports.GetExportedValue<IBlameLinkCommand>(),
66-
exports.GetExportedValue<ICopyLinkCommand>(),
67-
exports.GetExportedValue<ICreateGistCommand>(),
68-
exports.GetExportedValue<IOpenLinkCommand>(),
69-
exports.GetExportedValue<IOpenPullRequestsCommand>(),
70-
exports.GetExportedValue<IShowCurrentPullRequestCommand>(),
71-
exports.GetExportedValue<IShowGitHubPaneCommand>());
62+
// Avoid delays when there is ongoing UI activity.
63+
// See: https://github.com/github/VisualStudio/issues/1537
64+
await JoinableTaskFactory.RunAsync(VsTaskRunContext.UIThreadNormalPriority, async () =>
65+
{
66+
await JoinableTaskFactory.SwitchToMainThreadAsync();
67+
menuService.AddCommands(
68+
exports.GetExportedValue<IAddConnectionCommand>(),
69+
exports.GetExportedValue<IBlameLinkCommand>(),
70+
exports.GetExportedValue<ICopyLinkCommand>(),
71+
exports.GetExportedValue<ICreateGistCommand>(),
72+
exports.GetExportedValue<IOpenLinkCommand>(),
73+
exports.GetExportedValue<IOpenPullRequestsCommand>(),
74+
exports.GetExportedValue<IShowCurrentPullRequestCommand>(),
75+
exports.GetExportedValue<IShowGitHubPaneCommand>());
76+
});
7277
}
7378

7479
async Task EnsurePackageLoaded(Guid packageGuid)

0 commit comments

Comments
 (0)