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

Nov 17, 2011 at 1:34 PM
Edited Nov 18, 2011 at 7: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:

[threading.thread]::CurrentThread.GetApartmentState()
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:

STA
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
Done

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.

Developer
Nov 17, 2011 at 3: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.

Thanks

Doug

Nov 17, 2011 at 4: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

Coordinator
Nov 17, 2011 at 4: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 8: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?

Coordinator
Nov 18, 2011 at 3: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
http://HuddledMasses.org
http://PowerShellGroup.org