Reliably restart the application with one instance of WPF

I want to close the current application and restart it. I saw a lot of posts about this, but none of them seem to work for me.

I tried

System.Diagnostics.Process.Start(System.Windows.Application.ResourceAssembly.Location); System.Windows.Application.Current.Shutdown(); 

However, this only restarts the application once. If I press the restart button again (in an already restarted application), it will be closed.

I also tried to start a new System.Diagnostics.Process and close the current process, but again it does not restart, it just closes.

How to restart the current WPF application?

+4
source share
1 answer

You can create another application that you will start when you exit the application, and which in turn will launch your application again. It looks like the patcher will work, only without fixing anything. On the plus side, you can have a loop in this "restart-application" that checks all running processes for your main application process and only tries to restart it when it no longer appears in the process - and you got bare bones for the patcher too :) Although you have no problem restarting the application due to the fact that it is still in the process list, this is what I will do when I do this in a production environment, since it gives you the most control IMHO.

Edit:

This part of the button event handler (or where you want to restart the application) of your main application ( Process2BRestarted.exe in my case):

  private void cmdRestart_Click(object sender, EventArgs e) { var info = new ProcessStartInfo(); info.FileName = "ProcessReStarter"; info.WindowStyle = ProcessWindowStyle.Hidden; Process.Start(info); Application.Exit(); } 

This should go into your / restarter utility application ( ProcessReStarter.exe ):

  private void MainForm_Load(object sender, EventArgs e) { // wait for main application process to end // really should implement some kind of error-checking/timer here also while (Process.GetProcessesByName("Process2BRestarted").Count() > 0) { } // ok, process should not be running any longer, restart it Process.Start("Process2BRestarted"); // and exit the utility app Application.Exit(); } 

By clicking the restart button, you will create a new process, ProcessReStarter.exe , which will iterate over the list of processes of all running processes, checking that Process2BRestarted is still running. If the process no longer appears in the list, it will start the new Process2BRestarted.exe process and exit itself.

+2
source

All Articles