Skip to content

Commit

Permalink
Use asynchronous initialization to avoid switching to the UI thread e…
Browse files Browse the repository at this point in the history
…arlier than necessary
  • Loading branch information
sharwell committed May 13, 2018
1 parent 22dc6f2 commit ada00ab
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
25 changes: 19 additions & 6 deletions GitExtensionsVSIX/GitExtCommands.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Diagnostics;
using System.Threading;
using EnvDTE;
using GitExtensionsVSIX.Commands;
using Microsoft.VisualStudio.Shell;
using static GitExtensionsVSIX.PackageIds;
using Task = System.Threading.Tasks.Task;

namespace GitExtensionsVSIX
{
Expand All @@ -28,23 +31,25 @@ internal sealed class GitExtCommands

private readonly _DTE _application;
private OutputWindowPane _outputPane;
private readonly OleMenuCommandService _commandService;
private readonly IMenuCommandService _commandService;

/// <summary>
/// Initializes a new instance of the <see cref="GitExtCommands"/> class.
/// Adds our command handlers for menu (commands must exist in the command table file)
/// </summary>
/// <param name="package">Owner package, not null.</param>
private GitExtCommands(Package package)
private GitExtCommands(Package package, _DTE dte, IMenuCommandService menuCommandService)
{
ThreadHelper.ThrowIfNotOnUIThread();

if (package == null)
{
throw new ArgumentNullException(nameof(package));
}

_package = package;
_application = (_DTE)ServiceProvider.GetService(typeof(DTE));
_commandService = ServiceProvider.GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
_application = dte;
_commandService = menuCommandService;

try
{
Expand Down Expand Up @@ -130,9 +135,17 @@ private void MenuCommand_BeforeQueryStatus(object sender, EventArgs e)
/// Initializes the singleton instance of the command.
/// </summary>
/// <param name="package">Owner package, not null.</param>
public static void Initialize(Package package)
public static async Task InitializeAsync(AsyncPackage package, CancellationToken cancellationToken)
{
Instance = new GitExtCommands(package);
var dteObject = await package.GetServiceAsync(typeof(DTE));
var menuCommandServiceObject = await package.GetServiceAsync(typeof(IMenuCommandService));

cancellationToken.ThrowIfCancellationRequested();
Debug.Assert(dteObject != null, $"Assertion failed: {nameof(dteObject)} != null");
Debug.Assert(menuCommandServiceObject != null, $"Assertion failed: {nameof(menuCommandServiceObject)} != null");

await package.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);
Instance = new GitExtCommands(package, (_DTE)dteObject, (IMenuCommandService)menuCommandServiceObject);
}

/// <summary>
Expand Down
4 changes: 1 addition & 3 deletions GitExtensionsVSIX/GitExtensionsPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ public sealed class GitExtensionsPackage : AsyncPackage
protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
{
await base.InitializeAsync(cancellationToken, progress);
await JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);

GitExtCommands.Initialize(this);
await GitExtCommands.InitializeAsync(this, cancellationToken);
}
}
}

0 comments on commit ada00ab

Please sign in to comment.