How do I restart a WPF application?

86,876

Solution 1

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

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

Solution 2

I've used this in WPF, successfully:

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

Solution 3

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.GetEntryAssembly().Location + "\"";
Info.WindowStyle = ProcessWindowStyle.Hidden;
Info.CreateNoWindow = true;
Info.FileName = "cmd.exe";
Process.Start(Info);
Process.GetCurrentProcess().Kill();

EDIT:

I fixed the code:

instead of: Assembly.GetExecutingAssembly().Location

this: Assembly.GetEntryAssembly().Location

This is important when the function runs in a separate dll.

And -

instead of: Application.Current.Shutdown();

this: Process.GetCurrentProcess().Kill();

It will work both in WinForms and in WPF and if you write a dll that is designed for both then it is very important.

Solution 4

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.

Solution 5

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.

Share:
86,876
Hooch
Author by

Hooch

Updated on January 23, 2020

Comments

  • Hooch
    Hooch over 4 years

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

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

    How to do it in WPF?

  • Matěj Zábský
    Matěj Zábský about 13 years
    I think it's a bit overkill to include WinForms assembly reference just for this.
  • epalm
    epalm about 13 years
    @commanderz I'm inclined to agree, however I'm already using WinForms magic like System.Windows.Forms.FolderBrowserDialog() in the same project.
  • Hooch
    Hooch over 11 years
    @AndreiRinea No. But just save them at start and pass them as arguments to this function call. It is easy.
  • blachniet
    blachniet over 11 years
    Note that you don't want to use this method if your application is deployed with ClickOnce. The ApplicationDeployment.IsNetworkDeployed will be false when you restart. See bit.ly/RKoVBz for more info. If your application is not deployed with ClickOnce, this method works great.
  • Philliproso
    Philliproso over 10 years
    Writing a batch file with a sleep just feels like a long hack but possibly worse.
  • test-in-prod
    test-in-prod almost 10 years
    This answer (marked as answer) is misleading. The original question was about restarting a WPF application much akin to how it was done in WinForms. Unfortunately your answer is still using a WinForms restart (something that's not available in WPF) and also contains examples that are irrelevant to question being asked (like Mutex?).
  • JustADev
    JustADev almost 10 years
    This helped me avoid one simple issue, my startup args, my app crashed on any restart method I tried while this one kept things safe. Thank you :)
  • Hooch
    Hooch almost 10 years
    @blachniet Thanks. Took note of it.
  • John
    John almost 9 years
    @MatějZábský It's even necessary when using ClickOnce.
  • Mark
    Mark over 8 years
    @blachniet, what would be the effect to my application if ApplicationDeployment.IsNetworkDeployed is false?
  • blachniet
    blachniet over 8 years
    @ChristianMark, it may have no effect. It depends on your application. I have worked on some apps in the past that had both ClickOnce and normal installer deployments, and some parts of them behaved differently based on whether or not ApplicationDeployment.IsNetworkDeployed was true. For example, if you are ClickOnce deployed within an organization, you might then be able to infer that you should have access to organization network resources (file shares, email servers, etc.).
  • Mark
    Mark over 8 years
    Ahhh.. I see. Though my question is the possible code-level effect of this. Nevertheless, it worked.:) I do deploy my application only for organization network so I think it is not really an issue. I only use a shared database. File shares/emails doesn't really matter.
  • Jraco11
    Jraco11 over 8 years
    I can't tell you exactly why it works but only that it does in WPF
  • usefulBee
    usefulBee over 8 years
    Where do you get restart from? System.Windows.Application does not have a Restart method
  • Sebastien GISSINGER
    Sebastien GISSINGER over 8 years
    Maybe misleading for WPF-Only answer but useful in combination with other answers for me because I was needing to restart my WPF which uses a mutex. This does the job.
  • A. Zalonis
    A. Zalonis over 8 years
    Works perfect for me in WinForms!
  • CamHart
    CamHart over 8 years
    This is the ONLY way I've found to restart a ClickOnce application. Thanks.
  • Sandepku
    Sandepku about 8 years
    pretty freaking awesome!
  • dodgy_coder
    dodgy_coder over 7 years
    @Philliproso agreed, it is a hack, at the time I needed a completely new windows process for the restarted application, which the other methods here I believe do not provide, but this one does.
  • Ahmed Fwela
    Ahmed Fwela about 7 years
    if you want to preserve Command-line args , you can get them first from Environment.GetCommandLineArgs() then pass them to the Process.Start method
  • Eric Z
    Eric Z over 6 years
    Terrific! That's all I needed.
  • Matthieu Charbonnier
    Matthieu Charbonnier over 5 years
    @blachniet You should definitly post this answer in a case of an ClickOnce deployment.
  • Paul Karkoska
    Paul Karkoska almost 3 years
    Just tried this with my WPF app on .NET 4.8, and it is working perfectly. =]
  • Mark
    Mark over 2 years
    This didn't work for me until I change the location to .exe as in Application.ResourceAssembly.Location.Replace(".dll",".exe")