Skip to content

Commit 0c52beb

Browse files
Add commands on UI thread - fixes #242
1 parent 280e8d9 commit 0c52beb

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
All notable changes to the code converter will be documented here.
33

44
# 6.4.0 TBC
5+
Fix initialization bug in VS2017
56

67
### C# -> VB
78
* Tuples now converted

Vsix/ConvertCSToVBCommand.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Microsoft.VisualStudio.Shell;
1111
using Microsoft.VisualStudio.Text;
1212
using Microsoft.VisualStudio.Text.Editor;
13+
using Microsoft.VisualStudio.Threading;
1314
using OleMenuCommand = Microsoft.VisualStudio.Shell.OleMenuCommand;
1415
using OleMenuCommandService = Microsoft.VisualStudio.Shell.OleMenuCommandService;
1516
using Task = System.Threading.Tasks.Task;
@@ -63,7 +64,9 @@ IAsyncServiceProvider ServiceProvider {
6364
public static async Task InitializeAsync(REConverterPackage package)
6465
{
6566
CodeConversion codeConversion = await CodeConversion.CreateAsync(package, package.VsWorkspace, package.GetOptionsAsync);
66-
Instance = new ConvertCSToVBCommand(package, codeConversion, await package.GetServiceAsync<IMenuCommandService, OleMenuCommandService>());
67+
OleMenuCommandService oleMenuCommandService = await package.GetServiceAsync<IMenuCommandService, OleMenuCommandService>();
68+
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
69+
Instance = new ConvertCSToVBCommand(package, codeConversion, oleMenuCommandService);
6770
}
6871

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

Vsix/ConvertVBToCSCommand.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,10 @@ IAsyncServiceProvider ServiceProvider {
6060
/// <param name="package">Owner package, not null.</param>
6161
public static async Task InitializeAsync(REConverterPackage package)
6262
{
63-
CodeConversion codeConversion = await CodeConversion.CreateAsync(package, package.VsWorkspace, package.GetOptionsAsync);
64-
Instance = new ConvertVBToCSCommand(package, codeConversion, await package.GetServiceAsync<IMenuCommandService, OleMenuCommandService>());
63+
var codeConversion = await CodeConversion.CreateAsync(package, package.VsWorkspace, package.GetOptionsAsync);
64+
var oleMenuCommandService = await package.GetServiceAsync<IMenuCommandService, OleMenuCommandService>();
65+
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
66+
Instance = new ConvertVBToCSCommand(package, codeConversion, oleMenuCommandService);
6567
}
6668

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

0 commit comments

Comments
 (0)