I seem to have created an "immortal" process with the following piece of code:
System.Diagnostics.ProcessStartInfo test = new ProcessStartInfo("cmd", " /c " + aCommandLine); //aCommandLine is : ruby test.rb
test.CreateNoWindow = true;
test.RedirectStandardError = true;
test.RedirectStandardOutput = true;
test.UseShellExecute = false;
test.WorkingDirectory = System.IO.Path.GetDirectoryName(aRTextFilePath);
System.Diagnostics.Process aProcess = Process.Start(test);
aProcess.EnableRaisingEvents = true;
aProcess.Exited += new EventHandler(aProcess_Exited);
try
{
//read synchronously to get the port number
string aPortInformationLine = aProcess.StandardOutput.ReadLine();
if (!aProcess.HasExited)
{
aProcess.Kill();
Debug.Print("Process Has Exited");
}
//return true;
errorOut = String.Format("Could not start process with command line : {0} from .rtext file {1}", aCommandLine, aRTextFilePath);
port = -1;
return false;
}
catch (Win32Exception ex)
{
Debug.WriteLine(ex.NativeErrorCode.ToString());
errorOut = String.Format("Could not start process with command line : {0} from .rtext file {1}. Error : {2}", aCommandLine, aRTextFilePath, ex.NativeErrorCode.ToString());
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
errorOut = String.Format("Could not start process with command line : {0} from .rtext file {1}. Error : {2}", aCommandLine, aRTextFilePath, ex.Message);
}
So I am using cmd to start a ruby server of some sort. The process starts, and I am able to read output from the newly created process. However when the kill command is executed , no exceptions are thrown but the ruby process doesn't die.
Does this have anything to do with using the cmd ? Am I doing something else wrong? Thanks
Edit:
I used process explorer and took 2 pics before and after the kill:
Before:
After:
So while cmd.exe dies , ruby somehow manages to go on and live to see another day..
Edit2:
Answer to my problem: Kill process tree programmatically in C#