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.

ASYNC Main Does not Respect [STAThread]

See original GitHub issue

Version Used: 2017.3 Steps to Reproduce: Run this code:

[STAThread]
static async Task Main(string[] args) {
	Console.WriteLine(System.Threading.Thread.CurrentThread.ApartmentState);
}

Expected Behavior: I expect the thread to be STA.

Actual Behavior: The thread is MTA.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:13
  • Comments:18 (11 by maintainers)

github_iconTop GitHub Comments

10reactions
agockecommented, Jan 10, 2018

As mentioned in https://github.com/dotnet/csharplang/issues/97, this was explicitly considered and rejected by LDM. In addition, it sounds as though putting the attribute on the generated main method is not sufficient, anyway.

Regardless, this needs to be taken through the dotnet/csharplang repo and LDM before we’d consider a compiler change here.

7reactions
houseofcatcommented, Mar 24, 2022

I appreciate the consideration. My sole concern was to help people who come across this.

@RussKie

Read more comments on GitHub >

github_iconTop Results From Across the Web

Set ApartmentState for async void main
STAThread does not work with async main . This is a known and open issue. The problem is that the code that transforms...
Read more >
C# - async Main() STAThread ThreadStateException
The following code compiles but crashes with a ThreadStateException. It looks like async main() is not respecting the [STAThread] attribute.
Read more >
STAThread and async Main gotcha
And suddenly everything started to fell apart. I.e: System.Threading.ThreadStateException: 'Current thread must be set to single thread ...
Read more >
Calls to "async" methods should not be blocking
Making blocking calls to async methods transforms code that was intended to be asynchronous into a blocking operation. Doing so can cause deadlocks...
Read more >
F# High Performance - Page 151 - Google Books Result
Not just Windows Forms, the terms of operating under STAThread are also valid ... the Thread apartment model (including STA and MTA with...
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