0

I have an application where at one point I am closing all windows and I want to relaunch one of the closed windows. But the problem is when I say Window.Show(); the breakpoint shows the Actual Height and Actual Width to be 0 and after the line is executed, the entire app shuts down. Why, is it being garbage collected?

private void btnCancel_Click(object sender, RoutedEventArgs e) { if (jsonAssayViewModel.IsLiveProgress) { var res = Xceed.Wpf.Toolkit.MessageBox.Show( Application.Current.TryFindResource("CloseRun").ToString(), "Confirm dialog", MessageBoxButton.YesNo, MessageBoxImage.Question ); if (res == MessageBoxResult.No) { jsonAssayViewModel.PreventClosingWindow = true; return; } } //Raise the close event.... if (CloseCurrentProgressWindow != null) { CloseCurrentProgressWindow(); } var window = Window.GetWindow(this); if (window != null && window.Tag.ToString() == "Success") { if ((sender as Button).Content.ToString() == Application.Current.TryFindResource("Done").ToString()) { var AllWindows = Application.Current.Windows.Cast<Window>() .Where(win => win.IsLoaded); if (AllWindows.Count() > 2) { jsonAssayViewModel.SelectedAssay = null; jsonAssayViewModel.SelectedVolume = string.Empty; foreach (Window win in App.Current.Windows) { win.Close(); } NextGenDGRunSetupWindow wn = new NextGenDGRunSetupWindow(); wn.Show(); } } } } 

And in the NextGenDGRunSetupWindow I have the following

public NextGenDGRunSetupWindow() { InitializeComponent(); //idleTimeDetector = new IdleDetector(this); //1 minute readJsonViewModel = ReadJsonAssayViewModel.ReadJsonAssayViewModelInstance; //idleTimeDetector.IsIdle += IdleTime_IsIdle; readJsonViewModel.LaunchErrorWindowFromAnywhere += ReadJsonViewModel_LaunchErrorWindowFromAnywhere; } 
8
  • 2
    You do foreach (Window win in App.Current.Windows) {win.Close();}. This will also close your main program window -- which is akin to saying goodbye to your program, unless you change the ShutdownMode of your application. Commented Mar 16, 2018 at 22:43
  • Even if i move the NextGenDGRunSetupWindow wn = new NextGenDGRunSetupWindow(); wn.Show() logic before the foreach loop thats exiting the application as well. Ok I will try setting the ShutDownMode Commented Mar 16, 2018 at 22:51
  • Since i assume you are a beginner, i advise against changing ShutdownMode, as you would either need to kind of manually wire the shutdown logic or pay very close attention which windows you have to close when to shutdown your application. I guess it is a much easier approach to simply avoid closing the main window... Commented Mar 16, 2018 at 22:53
  • Thanks exactly what I am looking for, ShutdownMode="OnExplicitShutdown" works fine Commented Mar 16, 2018 at 22:54
  • You could however switch your main window to another window (that window then becoming the main window), by simply setting the Application.MainWindow property to the new main window. (This approach should then also take care of taskbar preview thumbnail, i hope...) Commented Mar 16, 2018 at 22:56

1 Answer 1

2

The problem is that you are closing every Window that's loaded in your application. Since Show() does not block, the moment the method finishes executing the startup Window closes and your application exits. You have a couple of options here:

  1. Use wn.ShowDialog(); - this blocks the current method until the window is closed; or
  2. Don't close the main window, whichever it is, in your foreach.
  3. Change Application.ShutDownMode to either ShutDownMode.OnExplicitShutdown or ShutDownMode.OnLastWindowClose
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks but setting the ShutDownMode in App.xaml worked fine. I guess thats an option too.
@NikhilANS I added that option as well

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.