You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Discussion: Defaults for Thread.ApartmentState #6908
This 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 an STA command line option? In our case we can workaround it by setting it ourselves by setting it in the InitialSessionState that we then pass to PowerShell.Create()
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.
Reacted by Christoph Bergmeister and Frank Lesniak
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.)
Thanks for the detailed explanation. Would it make sense to switch to STA as a default already before netcore3.0 to make breaking changes rather early than late?
Travis Plunk (@TravisEz13) For me it is sufficiently answered but you might want to consider opening an issue to re-implement -STA and/or -MTA parameters for pwsh.exe similar to powershell.exe and possibly make STA the default (now or only once netcore3.0 is available?)
Activity
iSazonov commentedon May 21, 2018
Christoph Bergmeister (@bergmeister) Please see very usefull discussion in #4772
(Also #4362)
PowerShell Committee Conclusion #4953
BrucePay commentedon May 21, 2018
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.
BrucePay commentedon May 21, 2018
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.)
bergmeister commentedon Jun 8, 2018
Thanks for the detailed explanation. Would it make sense to switch to STA as a default already before
netcore3.0to make breaking changes rather early than late?TravisEz13 commentedon Jun 30, 2018
Is this answered? Can I close it?
bergmeister commentedon Jun 30, 2018
Travis Plunk (@TravisEz13) For me it is sufficiently answered but you might want to consider opening an issue to re-implement
-STAand/or-MTAparameters for pwsh.exe similar to powershell.exe and possibly make STA the default (now or only once netcore3.0 is available?)TravisEz13 commentedon Jul 1, 2018
#7216 created