runtime: STAThread attribute is not respected in netcoreapp

Create a new netcoreapp2.0 project and set the Program.cs file to:

using System;
using System.Threading;

namespace ConsoleApp4
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            Console.WriteLine(Thread.CurrentThread.GetApartmentState());
            Console.ReadLine();
        }
    }
}

Examine that the output from the application is:

MTA

Not respecting STAThread means that users who depend on it (even if it really only matters on Windows), have to manually spawn a thread to run their code.

In some cases, certain libraries actually check and enforce that the calling thread is STA, and will throw an exception if it is not.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 2
  • Comments: 23 (23 by maintainers)

Most upvoted comments

I will try to reproduce it on my machine by running them in a loop to get more info about the cause of the failure, then after that we can discuss how to resolve it.

The fix was causing intermittent test failures - reverted in dotnet/coreclr#15586 .

@Anipik Let me know if you need help investigating the intermittent test failures. Since the failures are intermittent, you will probably need to run the affected tests in a loop to reproduce them.