Roslyn: ASYNC Main Does not Respect [STAThread]

Created on 14 Sep 2017  路  4Comments  路  Source: dotnet/roslyn

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.

Area-Compilers Bug

All 4 comments

When I look at the code in JustDecompile, this is what I see (You'll notice that the [STAThread] does not get copied to the "real" main:
````
private static void

(string[] args)
{
Program.Main(args).GetAwaiter().GetResult();
}

    [STAThread]
    private static async Task Main(string[] args)
    {
        Program.<Main>d__0 variable = null;
        AsyncTaskMethodBuilder asyncTaskMethodBuilder = AsyncTaskMethodBuilder.Create();
        asyncTaskMethodBuilder.Start<Program.<Main>d__0>(ref variable);
        return asyncTaskMethodBuilder.Task;
    }

````

FYI. In addition to this, there is also a more general issue for it not being respected in .NET Core: https://github.com/dotnet/coreclr/issues/13688

There was also some discussion of this issue (async main) here: https://github.com/dotnet/csharplang/issues/97

FYI @TyOverby

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.

Was this page helpful?
0 / 5 - 0 ratings