C#结束进程及子进程
作者:天方 时间:2021-06-22 15:18:10
这是个我在C#调用批处理文件时遇到的问题。首先我通过Process.Start方法调用一个批处理文件,那个批处理文件里面则调用了一大堆程序。当退出C#程序时,我在程序中结束杀掉了那个批处理文件的Process,但是,那个批处理所调用的子进程却无法像直接调用批处理文件那样随着批处理文件的进程一起被杀掉,而是自动向上提升成为了独立的进程。
在网上查了一下,可以通过NtQueryInformationProcess函数查询子进程的信息,并同时也查到了一段杀掉进程及所有子进程的C#代码,有需要的朋友可以参考一下。
static class ProcessExtend
{
// [StructLayout(LayoutKind.Sequential)]
private struct ProcessBasicInformation
{
public int ExitStatus;
public int PebBaseAddress;
public int AffinityMask;
public int BasePriority;
public uint UniqueProcessId;
public uint InheritedFromUniqueProcessId;
}
[DllImport("ntdll.dll")]
static extern int NtQueryInformationProcess(
IntPtr hProcess,
int processInformationClass /* 0 */,
ref ProcessBasicInformation processBasicInformation,
uint processInformationLength,
out uint returnLength
);
public static void KillProcessTree(this Process parent)
{
var processes = Process.GetProcesses();
foreach (var p in processes)
{
var pbi = new ProcessBasicInformation();
try
{
uint bytesWritten;
if (NtQueryInformationProcess(p.Handle, 0, ref pbi, (uint)Marshal.SizeOf(pbi), out bytesWritten) == 0) // == 0 is OK
if (pbi.InheritedFromUniqueProcessId == parent.Id)
using (var newParent = Process.GetProcessById((int)pbi.UniqueProcessId))
newParent.KillProcessTree();
}
catch { }
}
parent.Kill();
}
}
PS:今天发现NtQueryInformationProcess函数在x64位程序上运行无效, 具体原因不明,Google了一下也没有找到答案,反而找到了另一种解决方案,通过WMI来实现的。在x86和x64下都可以使用。
static void KillProcessAndChildren(int pid)
{
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);
Console.WriteLine(pid);
proc.Kill();
}
catch (ArgumentException)
{
/* process already exited */
}
}
来源:https://www.cnblogs.com/TianFang/archive/2010/05/19/1739614.html
标签:C#,结束,进程,子进程
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
一文带你了解Java选择排序的原理与实现
2022-05-13 21:01:31
![](https://img.aspxhome.com/file/2023/4/64784_0s.jpg)
Java 数据库连接池详解及简单实例
2023-08-10 15:54:07
springmvc分层领域模型概念详解
2021-09-07 01:21:51
![](https://img.aspxhome.com/file/2023/0/66660_0s.jpg)
C语言高效编程的几招小技巧
2023-11-02 14:12:50
C#基于TCP协议的服务器端和客户端通信编程的基础教程
2023-08-20 13:09:55
Java List分页功能实现代码实例
2022-06-02 13:56:14
Java对List进行排序的两种实现方法
2022-10-31 13:40:06
![](https://img.aspxhome.com/file/2023/8/65758_0s.png)
java队列实现方法(顺序队列,链式队列,循环队列)
2023-06-24 01:43:17
Android编程实现的首页左右滑动切换功能示例
2022-10-15 07:20:17
Java中List与数组之间的相互转换
2023-09-10 03:44:28
Android Google AutoService框架使用详解
2023-07-19 22:48:14
![](https://img.aspxhome.com/file/2023/3/89513_0s.png)
IDEA2022版本创建maven web项目的两种方式详解
2022-04-24 19:34:54
![](https://img.aspxhome.com/file/2023/5/84785_0s.jpg)
Java最全文件操作实例汇总
2023-11-14 13:00:17
SpringMVC拦截器创建配置及执行顺序
2023-06-06 20:41:16
![](https://img.aspxhome.com/file/2023/1/62351_0s.png)
C#实现文字转语音功能
2022-06-18 13:07:59
![](https://img.aspxhome.com/file/2023/6/91246_0s.jpg)
C#基础 延迟加载介绍与实例
2023-07-24 19:16:14
![](https://img.aspxhome.com/file/2023/5/104355_0s.jpg)
Android运用onTouchEvent自定义滑动布局
2021-09-24 04:39:06
![](https://img.aspxhome.com/file/2023/0/97880_0s.gif)
C#中使用@声明变量示例(逐字标识符)
2022-04-15 14:21:48
解析C#中的常量及如何在C#编程中定义常量
2022-03-20 22:58:36
Java接口DAO模式代码原理及应用详解
2023-06-21 05:29:04
![](https://img.aspxhome.com/file/2023/0/72570_0s.png)