Why is Application.Restart() not reliable?

Using the method Application.Restart() in C# should restart the current application: but it seems that this is not always working.

Is there a reason for this Issue, can somebody tell me, why it doesn't work all the time?


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've put something in their code that would stop the application from automatically shutting down, or starting up. Two examples:

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

Check your code for gotchas like that. If you're seeing this behaviour within a blank application, then that's more likely to be a problem with the actual function than your code.

Check Microsoft's sourcecode of application restart.


In my case (NO single-instance), where

Application.Restart();

didn't work,

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

did the job!


The only time I've run into this kind of issue is when in my main form I had a custom FormClosing event handler, that performed logic and canceled the event.

EDIT:

I have now run into another instance and based on your comments it possibly mirrors what you were experiencing.

When running a single instance application, using a Mutex, I was calling Application.Restart() from a fairly embedded location, that had a lot of cleanup to do. So it seems the restart was launching a new instance before the previous instance was complete, so the Mutex was keeping the new instance from starting.