0

I have a WPF application and I'm trying to do some async stuff in the startup. I'm trying to move from:

[STAThread]
public static void Main(string[] args)
{
    _app = new Application
    {
        ShutdownMode = ShutdownMode.OnExplicitShutdown
    };
    _app.Exit += AppOnExit;
    _app.DispatcherUnhandledException += BoomAd;

    SyncInitialization();

    _app.Run(new MainWindow(startupFiles));
}

To

[STAThread]
public static async Task Main(string[] args)
{
    _app = new Application
    {
        ShutdownMode = ShutdownMode.OnExplicitShutdown
    };
    _app.Exit += AppOnExit;
    _app.DispatcherUnhandledException += BoomAd;

    await InitializeAsync().ConfigureAwait(true);

    _app.Run(new MainWindow(startupFiles));
}

However, one of my initialization pieces stores the UI dispatcher for use later, and it validates that the dispatcher that it gets is STA. When I have the first code (void Main) it works fine, but when I have the second code (async Task Main) the dispatcher it gets is MTA. I have also looked in debugger and _app.Dispatcher.Thread is an MTA thread...

So what am I doing wrong that it's MTA? How do I get this type of startup correct?

3
  • 3
    stackoverflow.com/q/47553489, github.com/dotnet/roslyn/issues/22112 Commented Sep 9 at 20:00
  • Ah, was not aware of this thank you. If you make it an answer I will accept. Commented Sep 10 at 1:07
  • Assuming the InitializeAsync call could be made before creating and running the Application, you could simply do the latter in a dedicated STA thread, after awaiting InitializeAsync. Otherwise I'd say it doesn't make sense in the first place to start a WPF Application from an async Main method. Commented Sep 10 at 10:17

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.