The calling thread must be STA, because many UI components require this in WPF

Normally, the entry point method for threads for WPF have the [STAThreadAttribute] set for the ThreadMethod, or have the apartment state set to STA when creating the thread using Thread.SetApartmentState(). However, this can only be set before the thread is started.

If you cannot apply this attribute to the entry point of the application of thread you are performing this task from, try the following:

void Installer1_AfterInstall(object sender, InstallEventArgs e)
{
    var thread = new Thread(new ThreadStart(DisplayFormThread));

    thread.SetApartmentState(ApartmentState.STA);
    thread.Start();
    thread.Join();
}

private void DisplayFormThread()
{
    try
    {
        MainWindow ObjMain = new MainWindow();
        ObjMain.Show();
        ObjMain.Closed += (s, e) => System.Windows.Threading.Dispatcher.ExitAllFrames();

        System.Windows.Threading.Dispatcher.Run();
    }
    catch (Exception ex)
    {
        Log.Write(ex);
    }
}

Leave a Comment