Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restart an application by itself

I want to build my application with the function to restart itself. I found on codeproject

ProcessStartInfo Info=new ProcessStartInfo();
Info.Arguments="/C choice /C Y /N /D Y /T 3 & Del "+
               Application.ExecutablePath;
Info.WindowStyle=ProcessWindowStyle.Hidden;
Info.CreateNoWindow=true;
Info.FileName="cmd.exe";
Process.Start(Info); 
Application.Exit();

This does not work at all... And the other problem is, how to start it again like this? Maybe there are also arguments to start applications.

Edit:

http://www.codeproject.com/script/Articles/ArticleVersion.aspx?aid=31454&av=58703
like image 985
Noli Avatar asked Mar 07 '12 15:03

Noli


3 Answers

I use similar code to the code you tried when restarting apps. I send a timed cmd command to restart the app for me like this:

ProcessStartInfo Info = new ProcessStartInfo();
Info.Arguments = "/C ping 127.0.0.1 -n 2 && \"" + Application.ExecutablePath + "\"";
Info.WindowStyle = ProcessWindowStyle.Hidden;
Info.CreateNoWindow = true;
Info.FileName = "cmd.exe";
Process.Start(Info);
Application.Exit(); 

The command is sent to the OS, the ping pauses the script for 2-3 seconds, by which time the application has exited from Application.Exit(), then the next command after the ping starts it again.

Note: The \" puts quotes around the path, incase it has spaces, which cmd can't process without quotes.

Hope this helps!

like image 145
Bali C Avatar answered Sep 24 '22 01:09

Bali C


Why not use

Application.Restart();

??

More on Restart

like image 27
Shai Avatar answered Sep 24 '22 01:09

Shai


Why not just the following?

Process.Start(Application.ExecutablePath); 
Application.Exit();

If you want to be sure the app does not run twice either use Environment.Exit(-1) which kills the process instantaneously (not really the nice way) or something like starting a second app, which checks for the process of the main app and starts it again as soon as the process is gone.

like image 29
Christoph Fink Avatar answered Sep 25 '22 01:09

Christoph Fink