10000 Discussion: Defaults for Thread.ApartmentState · Issue #6908 · PowerShell/PowerShell · GitHub
[go: up one dir, main page]

Skip to content

Discussion: Defaults for Thread.ApartmentState #6908

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
bergmeister opened this issue May 21, 2018 · 7 comments
Closed

Discussion: Defaults for Thread.ApartmentState #6908

bergmeister opened this issue May 21, 2018 · 7 comments
Labels
Issue-Question ideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a Resolution-Answered The question is answered.

Comments

@bergmeister
Copy link
Contributor

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()
@iSazonov
Copy link
Collaborator
iSazonov commented May 21, 2018

@bergmeister Please see very usefull discussion in #4772
(Also #4362)

PowerShell Committee Conclusion #4953

@iSazonov iSazonov added the Issue-Question ideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a label May 21, 2018
@BrucePay
Copy link
Collaborator

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 seem 8000 s 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
Copy link
Collaborator

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
Copy link
Contributor Author

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?

@TravisEz13
Copy link
Member

Is this answered? Can I close it?

@bergmeister
Copy link
Contributor Author

@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?)

@TravisEz13
Copy link
Member

#7216 created

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Question ideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a Resolution-Answered The question is answered.
Projects
None yet
Development

No branches or pull requests

4 participants
0