我有以下代码
log.Info("Starting jar");
System.Diagnostics.ProcessStartInfo si = new ProcessStartInfo(procName);
si.RedirectStandardOutput = true;
si.RedirectStandardError = true;
si.UseShellExecute = false;
si.CreateNoWindow = false;
si.WorkingDirectory = System.IO.Directory.GetParent(Application.ExecutablePath) + "\\" + Properties.Settings.Default.rootDirectory;
//start a new process for Client
Process process = new Process();
process.StartInfo = si;
process.Start();
String name = process.ProcessName;
javaClientProcessId = process.Handle;
int javaProcessID = process.Id;
通过使用此代码我得到cmd作为进程名称在taskManager中显示为java.exe.从代码中它将5412作为process.id而1029作为process.Handle,其中6424是java.exe的进程ID是否有任何其他方法,我可以获得在TaskManager中的相同进程ID
注意
procName是运行jar文件的Bat文件的路径.
EDITED
当我执行以下代码时,它会从process.Kill()行给出错误.
if (process != null)
{
process.Kill();
process.Close();
process.Dispose();
}
由于进程(6504)已退出,因此无法处理请求
最佳答案 这是我的代码片段
try
{
Process[] javaProcList = Process.GetProcessesByName("java");
foreach (Process javaProc in javaProcList)
{
javaProc.Kill();
javaProc.Close();
javaProc.Dispose();
Console.WriteLine("StopJar -Java Process Stopped ");
log.Debug("StopJar -Java Process Stopped ");
}
}
catch (Exception exp)
{
log.Error("StopJar - Unable to kill Java Process", exp);
Console.WriteLine("Error while closing: " + exp.Message);
}