2

I am facing a strange situation where I am getting the following exception, I was running some test code with c# 7.1

"The calling thread must be STA, because many UI components require this."

Here is the code which throws the exception :

        public async Task StartAsync()
    {
        try
        {
            await Task.Yield();
            MainWindow mw = new MainWindow();
            mw.Show();
            mw.Activate();
        }
        catch (Exception exp)
        {
            int why= 0;
        }

    }

    [STAThread]
    public static async  Task Main()
    {
        var application = new AppNoUri() { ShutdownMode = ShutdownMode.OnExplicitShutdown };
        SynchronizationContext.SetSynchronizationContext(new DispatcherSynchronizationContext());
        application.Dispatcher.UnhandledException += Dispatcher_UnhandledException;
        application.InitializeComponent();
        Program p = new Program();
        p.StartAsync();
        application.Run();
    }

I have made sure that the MainThread is the thread calling the below line:

MainWindow mw  = new MainWindow()  is the main thread(this is the line that throws the above exception). 

Strangely

I change the signature of the main function to:

   [STAThread]
    public static void Main()

There is no exception and the code works fine. Can someone please advise as to why the case ?

Ps: I have seen this link with the same exception and but it was not helpful in my case. Also, I am using VS2017

dman
  • 145
  • 13
  • Main is called by the runtime, and I think it doesn't make sense to return anything other than int. – AnjumSKhan Feb 18 '18 at 07:04
  • Possible duplicate of [The calling thread must be STA, because many UI components require this](https://stackoverflow.com/questions/2329978/the-calling-thread-must-be-sta-because-many-ui-components-require-this) – Paul Karam Feb 18 '18 at 07:09
  • If you take a look at this link, you would understand your problem. Having async in your method declaration means this method will run asynchronously, while all methods invoked by the application on the startup must be single threaded. Your main function is asynchronous and calling another asynchronous function which leads to having multiple threads. Therefore, the exception. – Paul Karam Feb 18 '18 at 07:12
  • You are looking for an async application startup (initialization without blocking)? – Sir Rufo Feb 18 '18 at 11:36
  • 2
    WPF has a runtime check to ensure you don't create a UI component on a worker thread. It cannot in general know which specific thread is going to be the one that runs the dispatcher (Application.Run). There is an execution order issue, a program first creates UI objects, then starts the dispatcher. So it needs another way to find out, it calls Thread.GetApartmentState() under the hood. Apartments are a pure COM concept, but it is also useful to WPF to obtain this info. The [STAThread] attribute on Main() selects it. Normally auto-generated but your are hoofing it yourself here. – Hans Passant Feb 18 '18 at 13:32

0 Answers0