How to get ShowUI working in a custom PowerShell host in Visual Studio

Nov 17, 2011 at 2:34 PM
Edited Nov 18, 2011 at 8:18 AM

Hi there, I have a custom toolwindow in Visual Studio 2010 that contains a custom PowerShell host.
I would like to know if/how ShowUI can work in a VS context. I'm using the latest ShowUI version, 1.3.

When I run the following code in PowerGui, it shows that the apartment state is STA, shows the label, and shows "Done" after I close the label window:

Import-Module C:\Users\vincenth\Documents\WindowsPowerShell\Modules\ShowUI\ShowUI.psm1
New-Label "Hello World" -Show
Write-Host "Done"

When I run the same code in NuGet Package manager, it shows MTA and ShowUI tells me that it needs STA. So I changed my custom host from MTA to STA.
Now when I run the same code in my custom host in Visual Studio, it also shows STA, but it shows no visible window and produces no errors. Instead I get output:

Loading module from path 'C:\Users\vincenth\Documents\WindowsPowerShell\Modules\ShowUI\ShowUI.psm1'.
Loading module from path 'C:\Users\vincenth\Documents\WindowsPowerShell\Modules\ShowUI\GeneratedAssemblies\ShowUI.CLR4.0.30319.225.dll'.
Importing cmdlet 'New-DependencyObject'.
Importing cmdlet 'New-DispatcherSynchronizationContext'.

Importing alias 'XmlDataProvider'.
Control: System.Windows.Controls.Label
Type: System.String
Property: System.Object

Using -AsJob to create a thread does not work either.

Basically I have 2 questions

  1. Can ShowUI work in a VS2010 context?
  2. If so, what does ShowUI require from the PowerShell host?

Any example code of a custom PowerShell host where ShowUI works would be great.

Nov 17, 2011 at 4:11 PM

NuGet Package manager does not support STA mode. I logged and issue and Oisin, another PowerShell MVP working on that project, tried to get the STA mode for their last build. Unfortunately setting that mode in that console causes other issues.

It'd be good if you voted or opened this again on that project.



Nov 17, 2011 at 5:02 PM

I'll do that. Do you have a link/id for the issue that you logged?

However, my need is to get ShowUI working in my custom host that already supports STA mode. Any ideas on what more is required there?

Thanks, Vincent

Nov 17, 2011 at 5:42 PM

I'll have to dig into that one. I know we've been complaining about NuGet and threading since it came out, and they've been complaining about the Visual Studio threading. You should be able to use -AsJob, because that will create a new thread of it's own.

Nov 18, 2011 at 9:01 AM

Nope, I already tried -AsJob as well but that does not work either. Forgot to mention that, updated my question now. 

An approach could be to write a minimal custom host in a console app host and get that working with ShowUI outside of Visual Studio.
Then you could launch that same host in Visual Studio context (from e.g. the NuGet Package Manager console, not sure if you can launch a STA host from within a MTA host though...) and get it working in that context.

Possibly the issue is not related to threading but to the host wireup, the parent UI (does ShowUI need to find a 'parent window' ?) or the containing process?

Nov 18, 2011 at 4:07 PM
Not sure.
I believe PowerBoots worked even from NuGet -- but I'll have to test that, and if so, check what we're doing differently in AsJob.
They definitely work in PoshConsole, but that won't run inside Studio.

One thing you could try is do $Error.Clear(), run it and after "nothing" happens, check $Error again. There are a couple of bugs where we're swallowing errors that we should not swallow.

Joel "Jaykul" Bennett