Skip to content

Commit

Permalink
Add commands on UI thread - fixes #242
Browse files Browse the repository at this point in the history
  • Loading branch information
GrahamTheCoder committed Feb 7, 2019
1 parent 280e8d9 commit 0c52beb
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
All notable changes to the code converter will be documented here.

# 6.4.0 TBC
Fix initialization bug in VS2017

### C# -> VB
* Tuples now converted
Expand Down
7 changes: 6 additions & 1 deletion Vsix/ConvertCSToVBCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Threading;
using OleMenuCommand = Microsoft.VisualStudio.Shell.OleMenuCommand;
using OleMenuCommandService = Microsoft.VisualStudio.Shell.OleMenuCommandService;
using Task = System.Threading.Tasks.Task;
Expand Down Expand Up @@ -63,7 +64,9 @@ IAsyncServiceProvider ServiceProvider {
public static async Task InitializeAsync(REConverterPackage package)
{
CodeConversion codeConversion = await CodeConversion.CreateAsync(package, package.VsWorkspace, package.GetOptionsAsync);
Instance = new ConvertCSToVBCommand(package, codeConversion, await package.GetServiceAsync<IMenuCommandService, OleMenuCommandService>());
OleMenuCommandService oleMenuCommandService = await package.GetServiceAsync<IMenuCommandService, OleMenuCommandService>();
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
Instance = new ConvertCSToVBCommand(package, codeConversion, oleMenuCommandService);
}

/// <summary>
Expand All @@ -73,8 +76,10 @@ public static async Task InitializeAsync(REConverterPackage package)
/// <param name="package">Owner package, not null.</param>
/// <param name="codeConversion"></param>
/// <param name="commandService"></param>
/// <remarks>Must be called on the UI thread due to VS 2017's implementation of AddCommand which calls GetService</remarks>
ConvertCSToVBCommand(REConverterPackage package, CodeConversion codeConversion, OleMenuCommandService commandService)
{
ThreadHelper.ThrowIfNotOnUIThread();
this._package = package ?? throw new ArgumentNullException(nameof(package));
_codeConversion = codeConversion;

Expand Down
8 changes: 6 additions & 2 deletions Vsix/ConvertVBToCSCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ IAsyncServiceProvider ServiceProvider {
/// <param name="package">Owner package, not null.</param>
public static async Task InitializeAsync(REConverterPackage package)
{
CodeConversion codeConversion = await CodeConversion.CreateAsync(package, package.VsWorkspace, package.GetOptionsAsync);
Instance = new ConvertVBToCSCommand(package, codeConversion, await package.GetServiceAsync<IMenuCommandService, OleMenuCommandService>());
var codeConversion = await CodeConversion.CreateAsync(package, package.VsWorkspace, package.GetOptionsAsync);
var oleMenuCommandService = await package.GetServiceAsync<IMenuCommandService, OleMenuCommandService>();
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
Instance = new ConvertVBToCSCommand(package, codeConversion, oleMenuCommandService);
}

/// <summary>
Expand All @@ -71,8 +73,10 @@ public static async Task InitializeAsync(REConverterPackage package)
/// <param name="package">Owner package, not null.</param>
/// <param name="codeConversion"></param>
/// <param name="commandService"></param>
/// <remarks>Must be called on the UI thread due to VS 2017's implementation of AddCommand which calls GetService</remarks>
ConvertVBToCSCommand(REConverterPackage package, CodeConversion codeConversion, OleMenuCommandService commandService)
{
ThreadHelper.ThrowIfNotOnUIThread();
this._package = package ?? throw new ArgumentNullException(nameof(package));
_codeConversion = codeConversion;

Expand Down

0 comments on commit 0c52beb

Please sign in to comment.