WPF线程错误(已处理无效操作异常)
本文关键字:无效 操作 异常 处理 线程 错误 WPF | 更新日期: 2023-09-27 18:14:53
我正在创建WPF项目,我有两个屏幕
1)加载屏幕2)主窗口
我的代码如下,从加载屏幕
private void Window_Loaded(object sender, RoutedEventArgs e)
{
loadingThread = new Thread(load);
loadingThread.SetApartmentState(ApartmentState.STA);
loadingThread.Start();
}
private void load()
{
Thread.Sleep(1000);
this.Dispatcher.Invoke(showDelegate, "Loading UI...");
Thread.Sleep(2000);
//do some loading work
this.Dispatcher.Invoke(hideDelegate);
Thread.Sleep(2000);
this.Dispatcher.Invoke(DispatcherPriority.Normal,
(Action)delegate() { this.CLose(); });
MainWindow Mw = new MainWindow();//Gives me the error
Mw.ShowDialog();
}
当我调用主窗口屏幕时,我得到错误"应用程序对象正在被关闭"。我相信,因为线程被关闭,我得到的错误,谁能告诉我还有什么方法可以调用我的主窗口
Thank You All
您正在关闭调用应用程序关闭命令的最后一个窗口。在关闭"此"窗口之前,创建并打开新窗口:
你可以修改:
this.Dispatcher.Invoke(DispatcherPriority.Normal,
(Action)delegate() { this.Close(); });
:
this.Dispatcher.Invoke(DispatcherPriority.Normal,
(Action)delegate()
{
MainWindow Mw = new MainWindow();
// Mw.ShowDialog(); I changed this line because it cannot be a dialog here.
Mw.Show();
this.Close();
});