Click here to Skip to main content
15,740,971 members
Articles / Programming Languages / C#
Posted 26 Oct 2012

Tagged as


10 bookmarked

Application.Restart() Does Not Work Properly?

Rate me:
Please Sign up or sign in to vote.
4.57/5 (9 votes)
28 Oct 2012CPOL1 min read
The key to restart application properly is where you should code "Application.Restart();" in


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 "Application.Restart();" in.    


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 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 there is a need to restart application or end application normally.

static class Program
    static void Main()

        Form1 fm1 = new Form1();

        if (fm1.ToRestart)

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;

Click to enlarge image


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

SuggestionTranslation to VB.NET Pin
Spritznkarli12-Jun-13 1:55
Spritznkarli12-Jun-13 1:55 
In VB.NET, the same functionality can be implemented as follows. In the file ApplicationEvents.vb (reachable via Project Properties -> Application -> View Application Events), the code to restart the application can be added to the method that handles its shutdown:
Partial Friend Class MyApplication

    ' Flag to indicate if the application shall be restarted when it is shut down.
    Public Property RestartOnShutdown As Boolean

    Public Sub MyApplication_Shutdown(sender As Object, e As System.EventArgs) Handles Me.Shutdown
        ' Check if application is to be restarted
        If Me.RestartOnShutdown Then
            ' Attempt to restart

            Catch ex As Exception
                ' Operation might have been canceled by the user
            End Try
        End If
    End Sub

End Class

From the outside, restarting is as easy as setting the flag and closing the form(s):
' Set flag to restart the application
My.Application.RestartOnShutdown = True

' Close the form

QuestionThank you for the explanation and detail Pin
ismamad29-Oct-12 1:11
ismamad29-Oct-12 1:11 
GeneralMy vote of 4 Pin
Christiaan Rakowski28-Oct-12 23:59
professionalChristiaan Rakowski28-Oct-12 23:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.