Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何关闭C中的所有子进程#_Java_C#_Selenium_Selenium Webdriver_Process - Fatal编程技术网

Java 如何关闭C中的所有子进程#

Java 如何关闭C中的所有子进程#,java,c#,selenium,selenium-webdriver,process,Java,C#,Selenium,Selenium Webdriver,Process,C#代码: 我正在从C#windows应用程序调用java.exe。java.exe用于测试某些网站。Java使用Selenium通过打开默认web浏览器来测试网页。它将打开3到10次浏览器并测试测试用例。我想在C#应用程序中添加停止按钮,当我们单击它时,它应该关闭java.exe以及那些打开的浏览器。怎么做 我试图通过使用像这样的exe名称来获取进程名称 ProcessStartInfo processStartInfo = new ProcessStartInfo(); processSta

C#代码:

我正在从C#windows应用程序调用java.exe。java.exe用于测试某些网站。Java使用Selenium通过打开默认web浏览器来测试网页。它将打开3到10次浏览器并测试测试用例。我想在C#应用程序中添加停止按钮,当我们单击它时,它应该关闭java.exe以及那些打开的浏览器。怎么做

我试图通过使用像这样的exe名称来获取进程名称

ProcessStartInfo processStartInfo = new ProcessStartInfo();
processStartInfo.Verb = "runas";        
processStartInfo.FileName = fileNameToExecute;
processStartInfo.Arguments = parameters;
Process process = new Process();
processStartInfo.UseShellExecute = false;
processStartInfo.CreateNoWindow = true;
processStartInfo.RedirectStandardOutput = true;
processStartInfo.RedirectStandardError = true;
process.StartInfo = processStartInfo;
process.Start();
process.WaitForExit();
process.Close();

但它不起作用。

找到
java.exe
进程的进程ID,然后启动
TASKKILL
,将
/T
参数作为外部进程,以杀死整个父进程和子进程树:

例如:

Process[] processName = Process.GetProcessesByName(fileName.Substring(0, fileName.LastIndexOf('.')));

在启动新进程之前,我已将processId存储在全局变量中


Process process = new Process();
processStartInfo.UseShellExecute = false;
processStartInfo.CreateNoWindow = true;
processStartInfo.RedirectStandardOutput = true;
processStartInfo.RedirectStandardError = true;

process.StartInfo = processStartInfo;
process.Start();
javaExeProcessId = process.Id; //global variable
process.WaitForExit();
process.Close();

我称之为KillProcessAndChildren(javaexecprocessid);在需要时终止当前进程及其子进程


Process process = new Process();
processStartInfo.UseShellExecute = false;
processStartInfo.CreateNoWindow = true;
processStartInfo.RedirectStandardOutput = true;
processStartInfo.RedirectStandardError = true;

process.StartInfo = processStartInfo;
process.Start();
javaExeProcessId = process.Id; //global variable
process.WaitForExit();
process.Close();


private static void KillProcessAndChildren(int pid)
{
    // Cannot close 'system idle process'.
    if (pid == 0)
    {
        return;
    }
    ManagementObjectSearcher searcher = new ManagementObjectSearcher
            ("Select * From Win32_Process Where ParentProcessID=" + pid);
    ManagementObjectCollection moc = searcher.Get();
    foreach (ManagementObject mo in moc)
    {
        KillProcessAndChildren(Convert.ToInt32(mo["ProcessID"]));
    }
    try
    {
        Process proc = Process.GetProcessById(pid);
        proc.Kill();
    }
    catch (Exception ex)
    {
        // Process already exited.
    }
}