From 0c52beb71894d628720b386296a6a28d5ba887a5 Mon Sep 17 00:00:00 2001 From: GrahamTheCoder Date: Thu, 7 Feb 2019 00:37:30 +0000 Subject: [PATCH] Add commands on UI thread - fixes #242 --- CHANGELOG.md | 1 + Vsix/ConvertCSToVBCommand.cs | 7 ++++++- Vsix/ConvertVBToCSCommand.cs | 8 ++++++-- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ede7192a..c740b1940 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/Vsix/ConvertCSToVBCommand.cs b/Vsix/ConvertCSToVBCommand.cs index f2ea6c61b..0b675518e 100644 --- a/Vsix/ConvertCSToVBCommand.cs +++ b/Vsix/ConvertCSToVBCommand.cs @@ -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; @@ -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()); + OleMenuCommandService oleMenuCommandService = await package.GetServiceAsync(); + await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); + Instance = new ConvertCSToVBCommand(package, codeConversion, oleMenuCommandService); } /// @@ -73,8 +76,10 @@ public static async Task InitializeAsync(REConverterPackage package) /// Owner package, not null. /// /// + /// Must be called on the UI thread due to VS 2017's implementation of AddCommand which calls GetService ConvertCSToVBCommand(REConverterPackage package, CodeConversion codeConversion, OleMenuCommandService commandService) { + ThreadHelper.ThrowIfNotOnUIThread(); this._package = package ?? throw new ArgumentNullException(nameof(package)); _codeConversion = codeConversion; diff --git a/Vsix/ConvertVBToCSCommand.cs b/Vsix/ConvertVBToCSCommand.cs index 3b35715cd..b74f8494f 100644 --- a/Vsix/ConvertVBToCSCommand.cs +++ b/Vsix/ConvertVBToCSCommand.cs @@ -60,8 +60,10 @@ IAsyncServiceProvider ServiceProvider { /// Owner package, not null. 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()); + var codeConversion = await CodeConversion.CreateAsync(package, package.VsWorkspace, package.GetOptionsAsync); + var oleMenuCommandService = await package.GetServiceAsync(); + await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); + Instance = new ConvertVBToCSCommand(package, codeConversion, oleMenuCommandService); } /// @@ -71,8 +73,10 @@ public static async Task InitializeAsync(REConverterPackage package) /// Owner package, not null. /// /// + /// Must be called on the UI thread due to VS 2017's implementation of AddCommand which calls GetService ConvertVBToCSCommand(REConverterPackage package, CodeConversion codeConversion, OleMenuCommandService commandService) { + ThreadHelper.ThrowIfNotOnUIThread(); this._package = package ?? throw new ArgumentNullException(nameof(package)); _codeConversion = codeConversion;