Discussion: Defaults for Thread.ApartmentState
See original GitHub issueThis is to discuss and potentially document current behaviour (that is inconsistent with Windows PowerShell). Maybe some porting work was forgotten since the APIs were initially not available in .Net Core 1.
When creating a new PowerShell instance using [Management.Automation.PowerShell]::Create()
(or when creating a new runspace using [runspacefactory]::CreateRunspace()
), then PowerShell Core and Windows PowerShell are both in MTA
mode. This seems to be reasonable.
When being in the shell of PowerShell Core
, the default ApartmentState is also MTA
, which seems OK as well at first.
However, Windows PowerShell is in STA
mode by default and powershell.exe
even has an MTA
and STA
switch, which PowerShell Core does not have.
My questions:
- Why was
STA
chosen to be the default when being in a shell or ConsoleHost of Windows PowerShell? If there was a reason, we should reconsider it for PowerShell Core. I should add though that this inconsistency could lead to a situation that is difficult to debug (we had a case where execution in the shell was fine but when using our PowerShell runner that uses the PowerShell APIs, we were experiencing threading issues and scratching our heads for some time what the reason could be until we found out about the inconsistency in Windows PowerShell) - Does
pwsh
need anSTA
command line option? In our case we can workaround it by setting it ourselves by setting it in theInitialSessionState
that we then pass toPowerShell.Create()
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Thread.ApartmentState and PowerShell Execution Thread
Recently someone, in internal discussion groups, asked if it is possible to set “ApartmentState” on the powershell's execution thread.
Read more >Thread.ApartmentState Property (System.Threading)
NET Framework versions 1.0 and 1.1, the ApartmentState property marks a thread to indicate that it will execute in a single-threaded or multithreaded...
Read more >ApartmentState of a C# application's thread? - C# Discussion ...
does anybody know what the default ApartmentState of a C# application is, if you do not set the [STAthread] attribute for your main...
Read more >Set ApartmentState for async void main
1. Created Extension Method for running thread as STA · 2. Created async main method with await to application method (no [STAThread] attribute)....
Read more >What is STAThread Attribute? | What Does [STAThread] Do?
[STAThread] attribute specifies the communication mechanism between the current thread and other threads that may want to talk to it via COM.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
And now that XAML for .NET Core 3 has been announced, we should make sure that we at least have the option to run STA (and preferably make it the default.)
PowerShell V1 was MTA because that was the .NET default. We didn’t really think about it much and just decided to go with the default. It turned out that STA was preferred for WinForms and absolutely required for XAML. (In practice, it seems that a fair number of people do build GUIs in PowerShell. Sapien even has a WinForms designer for PowerShell.) Since the ISE is a XAML application, it had to be STA. For consistency in experience we decided to also change the console host to be STA. But there might have been some things that required MTA so we gave you the option to start the console host with either MTA or STA. Likewise in the API, if you use the default behaviour of creating a new thread for each command, you can run individual commands either STA or MTA.