1010using Microsoft . VisualStudio . Shell ;
1111using Microsoft . VisualStudio . Text ;
1212using Microsoft . VisualStudio . Text . Editor ;
13+ using Microsoft . VisualStudio . Threading ;
1314using OleMenuCommand = Microsoft . VisualStudio . Shell . OleMenuCommand ;
1415using OleMenuCommandService = Microsoft . VisualStudio . Shell . OleMenuCommandService ;
1516using 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
0 commit comments