question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Discussion: Defaults for Thread.ApartmentState

See original GitHub issue

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()

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
BrucePaycommented, 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.)

2reactions
BrucePaycommented, 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found