BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Windows Programming
.NET 2.0+

Restarting a Windows Forms Application

It can be useful to immediately terminate a Windows Forms program and restart it. This can be used as part of a recovery process for an application that has raised an unexpected exception or following an automated updates to the software.

Restarting an Application

There are many reasons for stopping the execution of a program and starting a fresh instance. For example, you may have included an automated update process within your software, either using pre-existing technologies such as ClickOnce, or using a custom installer that you have developed in-house. Once an update has been applied, it is common to require that the application be restarted to take advantage of new features. Another occasion for restarting software is following an unexpected system exception that may have left your program in an unstable state.

The .NET framework's Application class in the System.Windows.Forms namespace includes a static method named Restart. This method, when called, immediately shuts down the program and starts a new instance. The new instance of the program is started in the same context as the previous one, avoiding potential security threats. If the software was originally executed using command-line arguments, the new copy also uses the same options.

Application.Restart();

The Restart method follows the standard procedure for closing the application. This can lead to problems when you include FormClosing events that permit the user to cancel the shutdown process. If, during a FormClosing event, the cancellation flag is set, the application will not be closed. However, a new instance may still be started. To avoid this problem you should not permit cancellation when performing this type of restart.

7 May 2009