Reloading/Restarting Software
I took over a C# project that another programmer developed a while back and he's no longer with us. For a number of reasons, after this program gets to a certain point and is waiting for more commands, I need to kill the process and load it up again. I'm still fairly new to C#, and I was wondering how I would go about closing/re-opening the program automatically. Would I have to make use of a .bat file for this or are there other methods I can use?

kastion8 reputationPosted Apr 22, 2008, 6:27 AM EDT
On my condition that I need to reload the program I execute:
System.Diagnostics.Process.Start(@"C:\reload_myprogram.bat");
In my batch file I have:
TASKKILL /F /T /IM MyProgram.exe
SET INJDIR=C:\MyDir
START %INJDIR%\MyProgram.exe
The only problem I have with this is the program runs through cmd prompt, and when I kill the process the cmd prompt stays open. I'll keep searching...any help would be greatly appreciated.
AlanRank #669 · 3.6K reputationPosted Apr 22, 2008, 8:02 AM EDT
Rather than use a batch file, I'd just use Process.Kill() to kill the process and then Process.Start() to restart it. For example, start up Wordpad and then run this program:
using System;
using System.Diagnostics;
using System.Threading;
class Test
{
static void Main()
{
Process[] procs = Process.GetProcessesByName("wordpad");
procs[0].Kill();
Thread.Sleep(5000); // wait 5 secs
Process.Start("wordpad.exe");
}
}
kastion8 reputationPosted Apr 22, 2008, 9:42 AM EDT
About your suggested method, can that actually work in my C# program though? What I mean is...if the process I'm running kills itself how does it start back up? Won't it stop executing and never get to that block of code? That's why I thought I had to use the .bat method.
AlanRank #669 · 3.6K reputationPosted Apr 22, 2008, 10:24 AM EDT
Sorry, I misread your starting post :(
For some reason, I thought you wanted to kill an external process that you were controlling from your C# program and then restart it again.
The batch file approach is probably the cleanest way to destroy and restart the current program so I'd stick with that.