0

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:

BeforeKill After:

AfterKill

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#

Community
  • 1
  • 1
FailedDev
  • 26,680
  • 9
  • 53
  • 73

3 Answers3

1

Your hope that killing the command processor will also kill any process started by it is an idle one, Windows just doesn't work that way. There is a mechanism for it in Windows, job objects, but you don't want to go there. Simply don't use cmd.exe, it doesn't add any value. Start ruby.exe directly.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Thanks for the answer. I figured that out, but the problem is that I have to use the command line arguments by reading a file including the "ruby" exe as they are. – FailedDev Nov 17 '12 at 17:31
0

From MSDN:

The Kill method executes asynchronously. After calling the Kill method, call the WaitForExit method to wait for the process to exit, or check the HasExited property to determine if the process has exited.

PeteGO
  • 5,597
  • 3
  • 39
  • 70
0

Please check whether the process has any thread that is not declared as background thread. And that process may have any infinite loop..