Home > Software design >  WPF app closing window with another thread
WPF app closing window with another thread

Time:04-25

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?

CodePudding user response:

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.

CodePudding user response:

Maybe this will help you.

        private async Task ThreadStartingPoint()
        {
             await Task.Run(() =>
             {
                 Application.Current.Dispatcher.Invoke(() =>
                 {
                     MainWindow mainWindow = newMainWindow();
                     mainWindow.Show();
                 });
             }); 
        }
  • Related