Why Application.Restart() is not working?

Since .NET Framework 2.0, it supports Application.Restart() to “shuts down the application and starts a new instance immediately” as what it introduced in MSDN. many of the developers are complaining that “Why Application.Restart() is not working?” Let us look in this first,There could be a lot of reasons for this. It’s not that the method doesn’t work; rather, many times programmers forget that they have put something in their code that would stop the application from automatically shutting down, or starting up. Here I am giving two examples:

  • The Closing event on a form can stop an app’s shutdown completely.
  • If you are doing checking for an already-running process, the old one may not be closing fast enough to allow the new one to start up.

Also many of the developers are saying that they are getting the error as “Operation was cancelled by the user“.The one and only reason for this issue is , you are placed the code in a wrong place.

I Hope the following sample code will help you to understand how and where we need to apply the application.restart() method.

By default, the entry point of a startup application is Program.Main(). In Main(), the main form instance will be created and used by Application.Run(). Hence, Application.Restart() should be coded within Program.Main(). Use a simple property to indicate whether need to restart application or end application normally.
               static class Program
                     static void Main()
                          Form1 fm1 = new Form1();


                        if (fm1.ToRestart)

In main form, e.g. Form1 as above sample code, just set property in need to indicate 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;

Leave a Reply

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