Since .NET Framework 2.0, it supports
Application.Restart() to "shut down the application and start a new instance immediately" as what it introduced in MSDN. Unfortunately, not all developers including myself could not use this function to restart application expectedly. The key to restarting an application properly is where you should code "
My use case is need to restart application once configuration has been changed. After referring to some forums' articles, some will guide us to code "
Application.Restart();" within some events, e.g., button click, tool strip menu item click. But, some developers feedback is failure. And, I got an exception "Operation was canceled by the user." After discussions, I realized that right place to code it in is the key to restart application successfully.
Hope the following code sample is helpful to you.
Using the Code
By default, the entry point of a startup application is
Main(), the main form instance will be created and used by
Application.Restart() should be coded within
Program.Main(). Use a simple property to indicate whether there is a need to restart application or end application normally.
static class Program
static void Main()
Form1 fm1 = new Form1();
In the main form, e.g.,
Form1 as in the above sample code, just set property to indicate whether the application should be restarted and/or enforce to close main form.
public partial class Form1 : Form
public bool ToRestart = false;
private void button1_Click(object sender, EventArgs e)
ToRestart = true;
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.