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

Commit a9473d8

Browse files
committed
Use alternative to JoinableTaskFactory.WithPriority
This following doesn't return until the GitHub pane has completed loading. await JoinableTaskFactory .WithPriority(VsTaskRunContext.UIThreadNormalPriority) .SwitchToMainThreadAsync();
1 parent e7c1f4d commit a9473d8

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

src/GitHub.Exports/Helpers/ThreadingHelper.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
using static Microsoft.VisualStudio.Threading.JoinableTaskFactory;
99
using static Microsoft.VisualStudio.Threading.AwaitExtensions;
1010
using System.Windows.Threading;
11+
using Microsoft.VisualStudio.Shell.Interop;
12+
using Microsoft.VisualStudio.Shell;
13+
using Task = System.Threading.Tasks.Task;
1114

1215
namespace GitHub.Helpers
1316
{
@@ -56,6 +59,17 @@ public static IAwaitable SwitchToPoolThreadAsync(TaskScheduler scheduler = null)
5659
new AwaitableWrapper(scheduler ?? TaskScheduler.Default);
5760
}
5861

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+
5973
class AwaitableWrapper : IAwaitable
6074
{
6175
Func<IAwaiter> getAwaiter;

src/GitHub.InlineReviews/PullRequestStatusBarPackage.cs

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

1213
namespace GitHub.InlineReviews
1314
{
@@ -27,13 +28,15 @@ protected override async Task InitializeAsync(CancellationToken cancellationToke
2728
var serviceProvider = (IGitHubServiceProvider)await GetServiceAsync(typeof(IGitHubServiceProvider));
2829
var barManager = new PullRequestStatusBarManager(usageTracker, serviceProvider);
2930

30-
log.Information("SwitchToMainThreadAsync");
31-
await JoinableTaskFactory
32-
.WithPriority(VsTaskRunContext.UIThreadNormalPriority)
33-
.SwitchToMainThreadAsync();
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();
3436

35-
log.Information("StartShowingStatus");
36-
barManager.StartShowingStatus();
37+
// Posting a task like this seems to work.
38+
await ThreadingHelper.RunOnMainThreadNormalPriority(() => barManager.StartShowingStatus());
3739
}
40+
3841
}
3942
}

0 commit comments

Comments
 (0)