Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell and Single Threaded Apartment

I have developed a small GUI tool in PowerShell using WPK.

I have the following start up command

powershell -ExecutionPolicy Unrestricted -file PowerTools.ps1 -noexit -sta

I am getting the following error.

New-Object : Exception calling ".ctor" with "0" argument(s): "The calling thread must be STA, because many UI components require this."
At \WindowsPowerShell\Modules\WPK\GeneratedControls\PresentationFramework.ps1:34
10 char:29
+         $Object = New-Object <<<<  System.Windows.Window
    + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodInvocationException
    + FullyQualifiedErrorId :     ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

From my Googling, STA stands up "single-threaded apartment", I noticed that that powershell.exe has the "-sta" flag, but that does not help either.

The essence of my ps1 file looks like the following

Import-Module WPK

function get-gui()
{
      New-Window  -WindowStartupLocation CenterScreen `
       -Width 1200 -Height 200 -Show {

      New-Grid -Rows 32, 32, 32, 32 -Columns 110, 200* {
            ...
          }
        }
    }
}

get-gui

Any hints please?

(I did spend quite a bit of time doing my own research)

like image 531
lingo_journey Avatar asked Oct 17 '25 16:10

lingo_journey


1 Answers

It it this simple! :) Thanks for your guidance, Kayasax!

powerShell -sta -file PowerTools.ps1
like image 55
lingo_journey Avatar answered Oct 19 '25 08:10

lingo_journey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!