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

huangapple go评论90阅读模式
英文:

How to close all child processes in C#

问题

C# 代码:

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();


我在 C# Windows 应用程序中调用了 java.exejava.exe 用于测试一些网站。Java 使用 Selenium 来通过打开默认的网络浏览器来测试网页。它会打开 3  10 次浏览器并测试测试用例。我想在 C# 应用程序中添加一个停止按钮,当点击该按钮时,它应该关闭 java.exe 以及那些已打开的浏览器。该怎么做?

我尝试通过使用 exe 名称来获取进程名称,就像这样:

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

但是它没有起作用。
英文:

C# code:

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();

I am calling a java.exe from C# windows application. java.exe is used to test some websites. Java using Selenium to test the webpages by opening default web browser. It will open 3 to 10 times browsers and test the test cases. I want to add stop button in C# application and when we click it then it should close java.exe as well as those opened browsers. How to do it?

I tried to get process names by using exe name like this

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

But it is not working.

答案1

得分: 1


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

process.StartInfo = processStartInfo;
process.Start();
javaExeProcessId = process.Id; // 全局变量
process.WaitForExit();
process.Close();

在启动新进程之前,我将进程 ID 存储在一个全局变量中。


private static void KillProcessAndChildren(int pid)
{
	// 无法关闭“系统空闲进程”。
	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)
	{
		// 进程已经退出。
	}
}

我调用了 KillProcessAndChildren(javaExeProcessId); 来在需要的地方终止当前进程及其子进程。

英文:

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();

Before starting a new process I have stored the processId in a global variable.


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.
	}
}


I called KillProcessAndChildren(javaExeProcessId); to kill the current process and its child processes wherever required.

答案2

得分: 0

查找您的 java.exe 进程的进程ID,并启动带有 /T 参数的 TASKKILL 作为外部进程,以终止父进程和子进程的整个进程树:

示例:

TASKKILL /PID 1230 /T

MSDN Taskkill

英文:

Find the process ID of your java.exe process and start TASKKILL with /T parameter as external process to kill the whole tree of parent and child processes:

Example:

TASKKILL /PID 1230 /T

MSDN Taskkill

huangapple
  • 本文由 发表于 2020年8月31日 18:20:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63668951.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定