在单独的线程中启动WPF窗口

本文关键字:启动 WPF 窗口 线程 单独 | 更新日期: 2023-09-27 18:00:22

我使用的以下代码在单独的线程中打开一个窗口

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
    }
    private void button1_Click(object sender, RoutedEventArgs e)
    {
        Thread newWindowThread = new Thread(new ThreadStart(() =>
        {
            // Create and show the Window
            Config tempWindow = new Config();
            tempWindow.Show();
            // Start the Dispatcher Processing
            System.Windows.Threading.Dispatcher.Run();
        }));
        // Set the apartment state
        newWindowThread.SetApartmentState(ApartmentState.STA);
        // Make the thread a background thread
        newWindowThread.IsBackground = true;
        // Start the thread
    }
}

如果我在一个方法中使用这段代码,它就会起作用。但当我按如下方式使用它时,我会得到一个错误:

public partial class App : Application
{
    #region Instance Variables
    private Thread newWindowThread;
    #endregion
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        newWindowThread = new Thread(new ThreadStart(() =>
        {
            // Create and show the Window
            Config tempWindow = new Config();
            tempWindow.Show();
            // Start the Dispatcher Processing
            System.Windows.Threading.Dispatcher.Run();
        }));
    }
    private void button1_Click(object sender, RoutedEventArgs e)
    {
        // Set the apartment state
        newWindowThread.SetApartmentState(ApartmentState.STA);
        // Make the thread a background thread
        newWindowThread.IsBackground = true;
        // Start the thread
    }
}

它抛出以下错误:

System.Threading.ThreadStateException
The state of the thread was not valid to execute the operation

这是什么原因造成的?

在单独的线程中启动WPF窗口

@d.moncada,@JPVenson,@TomTom对不起所有人,尤其是@d.moncada,你的回答让我意识到了我的真正错误,如果运行一次直到我的代码工作为止。但我真正的问题是,我试图在两个位置按下按钮1_Click,实际上我使用了一个计时器,该计时器调用了行的方法

 private void button1_Click(object sender, RoutedEventArgs e)

现在我的问题的解决方案是检测线程是否已经在C#.net中运行?

我认为问题是在Thread开始运行之后设置ApartmentState

尝试:

public partial class App : Application
{
    #region Instance Variables
    private Thread newWindowThread;
    #endregion
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        newWindowThread = new Thread(new Thread(() =>
        {
            // Create and show the Window
            Config tempWindow = new Config();
            tempWindow.Show();
            // Start the Dispatcher Processing
            System.Windows.Threading.Dispatcher.Run();
        }));
        // Set the apartment state
        newWindowThread.SetApartmentState(ApartmentState.STA);
        // Make the thread a background thread
        newWindowThread.IsBackground = true;
    }
    private void button1_Click(object sender, RoutedEventArgs e)
    {
        // Start the thread
        newWindowThread.Start();
    }
}

为什么要这么做?(几乎)没有理由在应用程序中运行第2个UI线程。。。如果您想要一个非模态的新窗口,请实例化您的窗口并调用show

为什么差一点?因为这是一个非常复杂的主题,除非你有巨大的预算来开发这种行为,否则你可以没有它