我似乎用以下代码创建了一个“不朽”的过程:
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);
}
所以我使用 cmd 来启动某种红宝石服务器。该过程开始,我能够从新创建的过程中读取输出。但是,当执行 kill 命令时,不会抛出异常,但 ruby 进程不会死亡。
这与使用 cmd 有什么关系吗?我做错了什么吗?谢谢
编辑:
我使用进程资源管理器并在杀死前后拍了两张照片:
前:
后:
因此,当 cmd.exe 死亡时,ruby 以某种方式设法继续并活着看到另一天..
编辑2:
回答我的问题:在 C# 中以编程方式杀死进程树