1

Hello!

In my WPF-app, when I am in my 2nd or 3rd child-window, I have a 'Log out'-button. When it's pressed, I want all open windows to be closed, and the parent window (LoginScreen) to be re-opened. Alternatively, a restart of the entire application. Either one is fine.

P.S.: I am not interested in implementing a MVVM-navigation system (I'll leave that for my next project).

Currently, I am closing all open windows with the following code:

private void CloseAllWindows()
      {
         for (int intCounter = App.Current.Windows.Count - 1; intCounter >= 0; intCounter--)
            App.Current.Windows[intCounter].Close();
      }

But I do not know how to keep, or re-open, the LoginScreen-window.

What I've tried:

If I'm in the first child form, just re-instantiating the LoginScreen-window works well. But I need a work-around for when I'm deeper in the application.

I tried:

Process.Start(Application.ResourceAssembly.Location);
Application.Current.Shutdown();

It does not work.

Implementing System.Windows.Forms and using the Application.Restart() method doesn't work either. I get a red squiggly line, even though others profess to get it working. Any tips for a work-around?

Thanks!

Ole M
  • 317
  • 1
  • 17
  • 1
    `Process.Start` is a way to restart the software. Unless this is really necessary you should rather [replace entry point](https://stackoverflow.com/q/6156550/1997232) of wpf application. Then you gain a full control what window and when to open. – Sinatr Jun 17 '21 at 13:00
  • It would be helpful for other how is ready to help you if you post an [MCVE](https://stackoverflow.com/help/minimal-reproducible-example) – Rekshino Jun 17 '21 at 13:02
  • You could try to restart the App with a parameter, which will indicate, that login window should be opened first. – Rekshino Jun 17 '21 at 13:08
  • @Sinatr, I'd rather not implement an extensive new feature. But I would love a re-start. My code does not re-start the application, though. It just closes it. Any tips? These two examples does not work: `Application.Current.Shutdown(); Process.Start(Environment.GetCommandLineArgs()[0]);` `Process.Start(Application.ResourceAssembly.Location); Application.Current.Shutdown();` – Ole M Jun 17 '21 at 13:15
  • @Rekshino, thank you for your suggestion, I'll research that. – Ole M Jun 17 '21 at 13:15
  • @Rekshino, 2nd thought, I'm not sure I understand. LoginScreen IS the start-up Window. I just need code to do a normal restart, but I can't get it to work (see my other comment). Thanks alot. – Ole M Jun 17 '21 at 13:22
  • The code you tried does work, my test app being restarted, I can't reproduce the issue. – Rekshino Jun 17 '21 at 13:59

1 Answers1

3

This method closes all Windows except the MainWindow:

private static void CloseAllWindowsExceptMainWindow()
{
    Application.Current.Windows
        .Cast<Window>()
        .Where(w => w != Application.Current.MainWindow)
        .ToList()
        .ForEach(w => w.Close());
}

You may change the comparison in Where in case you want to keep some other specific Window open, for example:

private static void CloseAllWindowsExceptLoginScreen()
{
    Application.Current.Windows
        .Cast<Window>()
        .Where(w => !(w is LoginScreen))
        .ToList()
        .ForEach(w => w.Close());
}
Clemens
  • 123,504
  • 12
  • 155
  • 268
  • Thank you very much! I am not familiar with the type of code you used in your example; but it worked! I thought it didn't, at first, but turns out I had forgotten my LoginScreen was hidden. :-) Appreciated! – Ole M Jun 17 '21 at 13:37
  • 1
    It's making use of the [Enumerable](https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable?view=net-5.0) class. – Clemens Jun 17 '21 at 13:47