8

How can I restart an application from within it?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Alex
  • 10,869
  • 28
  • 93
  • 165
  • It's extremely rare that you application should ever need to restart itself. The only conceivable scenario is to automatically install updates, but the most logical way to handle that is with a helper application that downloads the updates and installs them at the next launch. The same model used by popular apps like Google Chrome and Mozilla Firefox, not only to solve this issue but also because it's *far* more user-friendly. – Cody Gray - on strike Apr 21 '11 at 12:30
  • 1
    possible duplicate of [How do I restart my C# WinForm Application?](http://stackoverflow.com/questions/779405/how-do-i-restart-my-c-winform-application), also see [What's the best way for a .NET windows forms application to update itself?](http://stackoverflow.com/questions/150935/whats-the-best-way-for-a-net-windows-forms-application-to-update-itself) – Cody Gray - on strike Apr 21 '11 at 12:31
  • One possible reason to restart your app (that I have used previously) could be in a ClickOnce deployment, where you check to see if an update is available in your code, you can then handle the actual update by setting the app to check for updates before it runs and restart it so that the update is done for you. – Matt Wilko Apr 21 '11 at 12:34
  • It makes sense if it's a server application and it needs to run constantly, 24/7. Bugs and exceptions are unavoidable in every program, but we don't want our server to just die if something unexpected happen. Yes, most of the time we can handle exceptions, but some exceptions mean we don't have the data required to continue, the state is unstable, we could break things if we continued, so the restart is the most reasonable solution. – Harry Jun 21 '12 at 08:55
  • 1
    It's a bit more complex than you would think: http://stackoverflow.com/questions/779405/how-do-i-restart-my-c-winform-application – Teoman Soygul Apr 21 '11 at 12:27

1 Answers1

9

Application.Restart is what you are looking for I think. I used this on one project and have to say that it worked fine for me - but other people seem to have run into issues with this.

Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
  • 1
    Are you sure it doesn't provide the command line arguments again? The MSDN docs (that you linked) state that it will, and I've heard other people say the same, e.g over [here](http://stackoverflow.com/questions/779405/how-do-i-restart-my-c-sharp-winform-application#5086597) – Shaun Apr 04 '13 at 13:16
  • @Shaun - you are correct. I thought I had read that this wasn't the case. I will edit my answer accordingly. – Matt Wilko Apr 04 '13 at 13:23