3

I have a small problem, when I open a new window in WPF like so:

 private void button2_Click(object sender, RoutedEventArgs e) { var newWindow = new Main(); newWindow.Show(); } 

If I try to use Application.Current.Shutdown(); at the end of it my entire application shuts down rather than just my first initial window. So my question would be is there a way to open a new window while closing the previous window safely?

Thanks :)

9
  • what happens if you this.Close()? Commented Mar 22, 2012 at 18:16
  • this.close is not the solution? application.shutdown will shutdown the app! Commented Mar 22, 2012 at 18:17
  • actually, if this.Close() if that code is executed on the main form, it might actually close the whole thing.. it's been a while since I touched desktop development.. sorry Commented Mar 22, 2012 at 18:20
  • Will I be correct if I say you're trying to make multiple dialog-like windows with previous and next? if this is the case, it might be preferable to hide or change the contents. Also, you can simply close the previous window from the new window, something like 'this.Parent.Close()' should work. Commented Mar 22, 2012 at 18:25
  • O HELLO! this.Close does work while still opening and keeping open my new window and closing the old window! Commented Mar 22, 2012 at 18:26

3 Answers 3

3

I'd do something like this:

//Somewhere in your class YourOtherForm otherForm = null; //then, on the event handler private void button2_Click(object sender, RoutedEventArgs e) { if((otherForm.IsDisposed) || (null == otherForm)) { otherForm = new YourOtherForm(); // or, this is an MDI or something // otherForm = new YourOtherForm(this); // assuming you have an extra constructor to pass the parent } otherForm.Show(); this.Close(); // or this.Hide(); if it's the main form } 

Edit: I haven't tested this code tho..

Sign up to request clarification or add additional context in comments.

Comments

1

The only way to do this is to run the program externally (I will find the code to do this shortly). Otherwise, anything that is created from within the main application will be destroyed when the parent shuts down.

The code to spin up a new program:

System.Diagnostics.Process.Start("program.exe"); 

5 Comments

wait a second.. is that gonna open an entire new instance of the program? I don't know if I understand this solution..
You do not have to shut down your application using Process.Start, however the application that is spun up will act separately of the one that created it. Like I mentioned, once the parent dies, all children die. However, your question raises some other questions...maybe you can edit your question and elaborate more on what you are looking to accomplish...maybe another solution can be had :D
@MilkyWayJoe Yes, this will open an entirely new instance of the program. As already mentioned, there is NO way to shutdown the parent of an application and still have the children survive.
Justin is right hence when I call application.current.shutdown the entire build just ends and its no longer in running process but this.Close keeps the parent alive but only getting rid of the window!
At the time I wrote the comment, I didn't realize it was the main form (mdi parent or something), and like I said, it's been a while since I touched any desktop app..
0

You will need to change the ShutdownMode to OnLastWindowClose in your App.xaml.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.