How to use Application.Restart()

I have never used the function Application.Restart(), but I came across this SO question today about its seemingly mysterious behavior that it caused the application the be restarted twice (instead of once).

In the posted code, the function was called from the FormClosing event handler.

A little search turned up this article on CodeProject that also dealt with the problem, and sketches the correct solution.

So I created a little sample application that calls Application.Restart from different locations:

  • Program.Main()
  • FormMain.FormClosing()
  • Dialog.FormClosing()

The code:

static class Program
{
  [STAThread]
  static void Main()
  {
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    var f = new Form1();
    Application.Run(f);

    if (f.AppRestart)
      Application.Restart();
  }
}

public partial class Form1 : Form
{
  bool restart;
  public bool AppRestart;

  public Form1()
  {
    InitializeComponent();
  }

  private void btnDialog_Click(object sender, EventArgs e)
  {
    (new FrmRestart()).Show();
  }

  private void btnRestartFromForm_Click(object sender, EventArgs e)
  {
    restart = true;
    this.Close();
  }

  private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  {
    if (restart)
      Application.Restart();
  }

  private void btnRestartFromApp_Click(object sender, EventArgs e)
  {
    AppRestart = true;
    this.Close();
  }
}

public partial class FrmRestart : Form
{
  bool restart;

  public FrmRestart()
  {
    InitializeComponent();
  }

  private void FrmRestart_FormClosing(object sender, FormClosingEventArgs e)
  {
    if (restart)
      Application.Restart();
  }

  private void btnRestart_Click(object sender, EventArgs e)
  {
    restart = true;
    this.Close();
  }
}

The results from this experiment show

  • Program.Main() restarts 1 instance
  • FormMain.FormClosing() restarts 2 instances
  • Dialog.FormClosing() restarts 2 instances

This SO answer also analyses the stack trace and finds that Application.Restart also closes every open form, and thus invokes FormClosing, which in turn causes the application restart twice.

I find the proposed solution (and currently accepted answer) problematic

private bool isRestarted;

private void FormClosing(object sender, FormClosingEventArgs e)
{
    if (settingschanged && !isRestarted)
    {
        isRestarted = true;
        Application.Restart(); 
    }
}

because it works around a specified behavior of a framework, rather than use the framework as intended.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: