ASYNC Main Does not Respect [STAThread]
See original GitHub issueVersion 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:
- Created 6 years ago
- Reactions:13
- Comments:18 (11 by maintainers)
Top 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 >
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
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.
I appreciate the consideration. My sole concern was to help people who come across this.
@RussKie