WPF应用程序关闭窗口与另一个线程
我正在使用以下代码从聊天应用程序退出。 我有一个登录窗口(MainWindow)和一个消息窗口,以下代码正在工作。
private void exitButton_Click(object sender, RoutedEventArgs e)
{
//send the server you want to log out
Data msgToSend = new Data();
msgToSend.cmdCommand = Command.Logout;
msgToSend.strName = LoginName;
msgToSend.strMessage = null;
byte[] b = msgToSend.ToByte();
ClientSocket.Send(b);
Thread newWindowThread = new Thread(new ThreadStart(ThreadStartingPoint));
newWindowThread.SetApartmentState(ApartmentState.STA);
newWindowThread.IsBackground = false;
newWindowThread.Start();
Close();
}
private void ThreadStartingPoint()
{
MainWindow mainWindow = new MainWindow();
mainWindow.Show();
System.Windows.Threading.Dispatcher.Run();
}
代码从消息窗口正确退出并打开登录窗口,但是当我再次登录时,它会在InitializeComponent()上引发错误; 应用对象正在关闭。我认为这是因为我创建的新线程。关闭窗口时,如何停止该线程,还是可以以不同的方式打开一个新窗口?
I'm using the following code to exit from a chat application.
I have a login window(MainWindow) and a Message Window where the below code is working.
private void exitButton_Click(object sender, RoutedEventArgs e)
{
//send the server you want to log out
Data msgToSend = new Data();
msgToSend.cmdCommand = Command.Logout;
msgToSend.strName = LoginName;
msgToSend.strMessage = null;
byte[] b = msgToSend.ToByte();
ClientSocket.Send(b);
Thread newWindowThread = new Thread(new ThreadStart(ThreadStartingPoint));
newWindowThread.SetApartmentState(ApartmentState.STA);
newWindowThread.IsBackground = false;
newWindowThread.Start();
Close();
}
private void ThreadStartingPoint()
{
MainWindow mainWindow = new MainWindow();
mainWindow.Show();
System.Windows.Threading.Dispatcher.Run();
}
The code exit properly from the message window and opens the login window, but when I log into again, it throws an error at InitializeComponent(); that the application object is being shut down. I think this is because the new thread that I created. How can I stop that thread when I close the window, or can I open a new window with another thread in a different way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
关闭消息窗口并在新的STA线程上启动新窗口时,消息窗口的sta线程将终止:因为
application.shutdownmode
设置为onlastWindowClose
默认情况下,一旦关闭了原始UI线程(应用程序线程)的最后一个窗口
,应用程序将过渡到关闭状态。绝对没有理由启动登录窗口或其自己的UI线程中的任何其他窗口。
除非您有充分的理由这样做,否则不要创建多个UI线程。
推荐的解决方案是
在您的Multi sta线程方案中 (不建议):
为了防止在原始UI线程的最后一个
窗口
关闭后,该应用程序已关闭,您必须将应用程序的shutdownmode
设置为onexplicitsHutdown
:< strong> app.xaml
别忘了注册EG,
window.closed
处理程序现在明确调用application.current.shutdown()
关闭应用程序。When you close the message window and start a new window on a new STA thread, the STA thread of the message window will terminate: since the
Application.ShutdownMode
is set toOnLastWindowClose
by default, the application will transition into the shutdown state, once the lastWindow
of the original UI thread (application thread) is closed.There is absolutely no reason to start the login window or any other window in its own UI thread.
Don't create multiple UI threads unless you have a very good reason to do so.
The recommended solution is to
In your case of a multi STA thread scenario (not recommended):
To prevent the application from shutting down after the last
Window
of the original UI thread has closed, you must set the application'sShutdownMode
toOnExplicitShutdown
:App.xaml
Don't forget to register an e.g.,
Window.Closed
handler to now explicitly callApplication.Current.Shutdown()
to shut down the application.