10

Possible Duplicate:
Why is Application.Restart() not reliable?

I pulled the code straight from MSDN. This updates my application, but Restart() does not work. The application shuts down, but it does not restart.

I added a MenuItem to my Form to validate that Restart() works at all:

private void restartToolStripMenuItem_Click(object sender, EventArgs e)
{
    Application.Restart();
}

This will restart the application (of course, it performs no updates and is user initiated, so it is fairly useless).

I have nothing else going on with this application. No event handlers for the Form on shutdown, nothing. This is the most basic Windows Forms application I could build (it just displays a resource JPEG in an ImagePanel).

Why does Restart() not work here?

Community
  • 1
  • 1
Chris Holmes
  • 11,444
  • 12
  • 50
  • 64
  • See [this](http://stackoverflow.com/questions/95098/why-is-application-restart-not-reliable) other SO question. – uzbones Jun 12 '09 at 18:38

5 Answers5

5

Is your application Windows Forms or WPF? Because Application.Restart only exists in the Windows Forms Application object (System.Windows.Forms.Application) and is not supported by applications running under the WPF Application (System.Windows.Applications). You can still call it, but as the application context is different, it doesn't work.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Greg Bacchus
  • 2,235
  • 2
  • 23
  • 26
4

If you are using a Mutex, or something of the like to ensure only one instance of the application is running at a time, that be causing this issue.

Timothy Carter
  • 15,459
  • 7
  • 44
  • 62
1

Try wrapping it with a BeginInvoke just in case it's not on the main STA thread.

Community
  • 1
  • 1
Brandon Grossutti
  • 1,241
  • 5
  • 16
  • 24
0

Are you sure that you're calling Application.Restart from the main form? If you call a form with .ShowDialog and then from that form call Application.Restart, it won't work because the .ShowDialog causes the dialog form to run on a separate thread.

Matt Hanson
  • 3,458
  • 7
  • 40
  • 61
-1

Try to raise a new process, maybe that can workaround it:

Process.Start(Application.ExecutablePath);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jhonny D. Cano -Leftware-
  • 17,663
  • 14
  • 81
  • 103