How do I restart a WPF application?
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.
Hooch
Updated on January 23, 2020Comments
-
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ý about 13 yearsI think it's a bit overkill to include WinForms assembly reference just for this.
-
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 over 11 years@AndreiRinea No. But just save them at start and pass them as arguments to this function call. It is easy.
-
blachniet over 11 yearsNote 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 over 10 yearsWriting a batch file with a sleep just feels like a long hack but possibly worse.
-
test-in-prod almost 10 yearsThis 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 almost 10 yearsThis 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 almost 10 years@blachniet Thanks. Took note of it.
-
John almost 9 years@MatějZábský It's even necessary when using ClickOnce.
-
Mark over 8 years@blachniet, what would be the effect to my application if
ApplicationDeployment.IsNetworkDeployed
is false? -
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 over 8 yearsAhhh.. 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 over 8 yearsI can't tell you exactly why it works but only that it does in WPF
-
usefulBee over 8 yearsWhere do you get restart from? System.Windows.Application does not have a Restart method
-
Sebastien GISSINGER over 8 yearsMaybe 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 over 8 yearsWorks perfect for me in WinForms!
-
CamHart over 8 yearsThis is the ONLY way I've found to restart a ClickOnce application. Thanks.
-
Sandepku about 8 yearspretty freaking awesome!
-
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 about 7 yearsif 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 over 6 yearsTerrific! That's all I needed.
-
Matthieu Charbonnier over 5 years@blachniet You should definitly post this answer in a case of an ClickOnce deployment.
-
Paul Karkoska almost 3 yearsJust tried this with my WPF app on .NET 4.8, and it is working perfectly. =]
-
Mark over 2 yearsThis didn't work for me until I change the location to .exe as in Application.ResourceAssembly.Location.Replace(".dll",".exe")