I am working on a GUI in PowerShell, where I threw errors when I clicked certain combo boxes.
After the error has been reset, I can delete the combobox list and see its contents, but if I moved to another combox on the same datagridview, I would get the same initial error before I could see the drop-down list.
I posted this on the TechNet PowerShell forums and got the answer that I needed to run my GUI in a single-threaded apartment (STA). PowerShell works by default in MTA, but you can overwrite it (in version 2.0) with the -STA switch when invoking powershell.exe .
However, my GUI just calls the default PowerShell application (in MTA mode) , so my question is: is there a way to programmatically set the apartment property from my GUI / script?
If not, my next attempt would be to detect the state of the apartment and try to restart my GUI from the initial boot of my gi with something like:
powershell.exe -STA myguiprog.ps1
Edit:
So my solution works:
if ([threading.thread]::CurrentThread.GetApartmentState() -eq "MTA") { & $env:SystemRoot\system32\WindowsPowerShell\v1.0\powershell.exe -sta $MyInvocation.ScriptName }
source share