0

i try to Perform reset to my C# program like this:

System.Windows.Forms.Application.Restart();

also i have in the Program class this To ensure that there is only one instance

 [STAThread]
        static void Main()
        {
            if (Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Length > 1)
            {
                MessageBox.Show("the program is runing", "attention", 0, MessageBoxIcon.Hand);
                return;
            }
            else
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Main());

             }
}

The problem that when I run the program and Perform reset

System.Windows.Forms.Application.Restart(); 

i got message that the program is running Or nothing happens at all.

thanks

  • Unfortunately, you're [not the first to ask](http://stackoverflow.com/questions/14498633/application-restart-single-instance-conflict-in-windows-application), but no answers there yet either. – James Thorpe Mar 11 '15 at 12:52
  • [Another similar one](http://stackoverflow.com/questions/1063747/how-do-i-restart-a-single-instance-winforms-application) - talks about other threads possibly keeping it alive, are you spinning up extra threads at all? – James Thorpe Mar 11 '15 at 12:55
  • 1
    How about starting a separate process which is going to restart your app after some time, so you can terminate your original app? Or revising your SingleInstance procedure to create a mutex like here http://stackoverflow.com/questions/1063747/how-do-i-restart-a-single-instance-winforms-application] that you can release when restarting your app? – Marwie Mar 11 '15 at 13:19
  • Clearly you are going to need an *extra* global variable that says that you are about to call Restart(). So you can fix your Main() method, for a brief moment you'll have two instances of your app running. The one that called Restart() and should end very soon. And the one that was started by the Restart() call. – Hans Passant Mar 11 '15 at 13:26

0 Answers0