Home » c# » How do I restart a WPF application?

How do I restart a WPF application?

Posted by: admin November 29, 2017 Leave a comment

Questions:

How can I restart a WPF Application?
In windows Forms I used

System.Windows.Forms.Application.Restart();

How to do it in WPF?

Answers:

I found this:
It works.
But.
Is there any better way?

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

Questions:
Answers:

I’ve used this in WPF, successfully:

System.Windows.Forms.Application.Restart();
System.Windows.Application.Current.Shutdown();

Questions:
Answers:
Application.Restart();

or

System.Diagnostics.Process.Start(Application.ExecutablePath);
Application.Exit();

In my program I have a mutex to ensure only one instance of the application running on a computer. This was causing the newly started application to not start because the mutex had not been release in a timely fashion. As a result I put a value into Properties.Settings that indicates that the application is restarting. Before calling Application.Restart() the Properties.Settings value is set to true. In Program.Main() I also added a check for that specific property.settings value so that when true it is reset to false and there is a Thread.Sleep(3000);

In your program you may have the logic:

if (ShouldRestartApp)
{
   Properties.Settings.Default.IsRestarting = true;
   Properties.Settings.Default.Save();
   Application.Restart();
}

In Program.Main()

[STAThread]
static void Main()
{
   Mutex runOnce = null;

   if (Properties.Settings.Default.IsRestarting)
   {
      Properties.Settings.Default.IsRestarting = false;
      Properties.Settings.Default.Save();
      Thread.Sleep(3000);
   }

   try
   {
      runOnce = new Mutex(true, "SOME_MUTEX_NAME");

      if (runOnce.WaitOne(TimeSpan.Zero))
      {
         Application.EnableVisualStyles();
         Application.SetCompatibleTextRenderingDefault(false);
         Application.Run(new Form1());
      }
   }
   finally
   {
      if (null != runOnce)
         runOnce.Close();
   }
}

That’s it.

Questions:
Answers:
Application.Current.Shutdown();
System.Windows.Forms.Application.Restart();

In this order worked for me, the other way around just started another instance of the app.

Questions:
Answers:

Runs a new instance of the program by command line after 1 second delay.
During the delay current instance shutdown.

ProcessStartInfo Info = new ProcessStartInfo();
Info.Arguments = "/C choice /C Y /N /D Y /T 1 & START \"\" \"" + Assembly.GetExecutingAssembly().Location + "\"";
Info.WindowStyle = ProcessWindowStyle.Hidden;
Info.CreateNoWindow = true;
Info.FileName = "cmd.exe";
Process.Start(Info);
Application.Current.Shutdown();

Questions:
Answers:

These proposed solutions may work, but as another commenter has mentioned, they feel kind of like a quick hack. Another way of doing this which feels a little cleaner is to run a batch file which includes a delay (e.g. 5 seconds) to wait for the current (closing) application to terminate.

This prevents the two application instances from being open at the same time. In my case its invalid for two application instances to be open at the same time – I’m using a mutex to ensure there is only one application open – due to the application using some hardware resources.

Example windows batch file (“restart.bat”):

sleep 5
start "" "C:\Dev\MyApplication.exe"

And in the WPF application, add this code:

// Launch the restart batch file
Process.Start(@"C:\Dev\restart.bat");

// Close the current application
Application.Current.MainWindow.Close();

Questions:
Answers:
 Application.Restart();
 Process.GetCurrentProcess().Kill();

work like a charm for me

Leave a Reply

Your email address will not be published. Required fields are marked *