简体   繁体   中英

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

This error message jump out in a sudden without any change

I am using Application_Startup() as start up function to invoke MainWindow

 public partial class App : Application
{
    public string FileName;
    private void Application_Startup(object sender, StartupEventArgs e)
    {
        //Thread nthrd = new Thread(new ThreadStart(MainWindowStartupMethod));
        //nthrd.SetApartmentState(ApartmentState.STA);
        //nthrd.Start();
        //nthrd.Join();
        MainWindow wnd = new MainWindow();

The error is happening in the last line "public MainWindow()"

public partial class MainWindow : Window
{
    public WizardSetup data = new WizardSetup();
    public Connectiontest ConnectionObj;
    public delegate void ZoneFocusContentUpdateEventHandler(object sender, TextChangedEventArgs e);
    WizardProcessBar Processor;
    Point InitialPosition = new Point();//using for image translate
    public bool WizardToHWSucceed = false;
    public MainWindow()

I do know the main() function should be STA and it is showing here by system default:

    /// <summary>
    /// Application Entry Point.
    /// </summary>
    [System.STAThreadAttribute()]
    [System.Diagnostics.DebuggerNonUserCodeAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
    public static void Main() {
        TPACquisitionWizard.App app = new TPACquisitionWizard.App();
        app.InitializeComponent();
        app.Run();
    }

The project works with a new STA thread which is showing in comments in first code section, but it was working before to show MainWindow in main thread.Even I change "StartupUri = "MainWindow.xaml" in App.xaml does not work. So my question is how to go back to previous method using main thread

Based on the comments, it sounds like the easiest way to get this working would be to create a new WPF application and then copy in any useful code that you want to keep. You are writing code in dark, secret corners of your project where I suspect that code does not need to be written.

After you create your new project, immediately click "start" and make sure that it works. As you copy over code, ensure that any changes you are making does not cause the program to quit running.

EDIT: Here is my code from App.xaml in a working project

<Application x:Class="MyProject.App" 
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
             xmlns:vm="clr-namespace:MyProject.ViewModel"
             StartupUri="ControlPanel.xaml" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">

</Application>

Note the StartupUri - when I click "Start", I see ControlPanel.xaml. I have a resource dictionary in here (not shown), but not much custom code at all. Here is my app.xaml.cs

using System.Windows;
using GalaSoft.MvvmLight.Threading;

namespace MyProject
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        static App()
        {
            DispatcherHelper.Initialize();
        }
    }
}

The one line of code was autogenerated, I think by MvvmLight toolkit. I have not touched this file, and this is a production project...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM