WPF应用程序关闭窗口与另一个线程

发布于 2025-01-23 13:47:44 字数 1139 浏览 0 评论 0原文

我正在使用以下代码从聊天应用程序退出。 我有一个登录窗口(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一个人练习一个人 2025-01-30 13:47:44

关闭消息窗口并在新的STA线程上启动新窗口时,消息窗口的sta线程将终止:因为application.shutdownmode设置为onlastWindowClose默认情况下,一旦关闭了原始UI线程(应用程序线程)的最后一个窗口,应用程序将过渡到关闭状态。

绝对没有理由启动登录窗口或其自己的UI线程中的任何其他窗口。
除非您有充分的理由这样做,否则不要创建多个UI线程。

推荐的解决方案是

  1. 显示新窗口
  2. 关闭旧窗口

在您的Multi sta线程方案中 (不建议):
为了防止在原始UI线程的最后一个窗口关闭后,该应用程序已关闭,您必须将应用程序的shutdownmode设置为onexplicitsHutdown

< strong> app.xaml

<Application ShutdownMode="OnExplicitShutdown">
</Application>

别忘了注册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 to OnLastWindowClose by default, the application will transition into the shutdown state, once the last Window 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

  1. show the new Window
  2. close the old Window

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's ShutdownMode to OnExplicitShutdown:

App.xaml

<Application ShutdownMode="OnExplicitShutdown">
</Application>

Don't forget to register an e.g., Window.Closed handler to now explicitly call Application.Current.Shutdown() to shut down the application.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文