roslyn: ASYNC Main Does not Respect [STAThread]
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.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 13
- Comments: 18 (11 by maintainers)
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
Thank for roping me in.
Looking at this code I can agree that the behvaiour can totally look unexpected.
At the same time my main question about this code - what does it mean to start an async Windows Forms app? So while the behaviour is surprising, I don’t believe this would be a supported scenario. Just because the language support this construct, it doesn’t mean the SDK supports, and you should use it.
As far as the idea of an analyzer that would issue a warning when a developer tries to use
STAThread
withasync Main
- I think it may be a good idea.You can. For any project you are a maintainer of.
You get to determine what is acceptable/expected behavior and you can handle your bugs accordingly. Otherwise, it would not be possible for maintainers to close bugs as any user could say “i still think this is buggy, so you have to keep it open”.
I wish I could close software tickets on buggy software because its working as I expected it to.
Let me ask: Is the
STAThread
thread apartment attribute aboveMain
working whenMain
is anasync Task
orasync void
?If not then your expected design isn’t working. Now, the compiler could issue a
WARN
orERROR
to tell the developers that it isn’t included in the expected design and I really couldn’t say anything, because then IT WOULD be working according to your expected design as you actually alerted developers it doesn’t work.