網(wǎng)站首頁 編程語言 正文
這是個(gè)我在C#調(diào)用批處理文件時(shí)遇到的問題。首先我通過Process.Start方法調(diào)用一個(gè)批處理文件,那個(gè)批處理文件里面則調(diào)用了一大堆程序。當(dāng)退出C#程序時(shí),我在程序中結(jié)束殺掉了那個(gè)批處理文件的Process,但是,那個(gè)批處理所調(diào)用的子進(jìn)程卻無法像直接調(diào)用批處理文件那樣隨著批處理文件的進(jìn)程一起被殺掉,而是自動向上提升成為了獨(dú)立的進(jìn)程。
在網(wǎng)上查了一下,可以通過NtQueryInformationProcess函數(shù)查詢子進(jìn)程的信息,并同時(shí)也查到了一段殺掉進(jìn)程及所有子進(jìn)程的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:今天發(fā)現(xiàn)NtQueryInformationProcess函數(shù)在x64位程序上運(yùn)行無效,?具體原因不明,Google了一下也沒有找到答案,反而找到了另一種解決方案,通過WMI來實(shí)現(xiàn)的。在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
相關(guān)推薦
- 2022-09-22 Apriori算法的實(shí)現(xiàn)
- 2023-01-30 C++指針和數(shù)組:字符和字符串、字符數(shù)組的關(guān)聯(lián)和區(qū)別_C 語言
- 2022-08-24 python多線程死鎖現(xiàn)象及解決方法_python
- 2022-07-30 Redis?keys命令的具體使用_Redis
- 2022-07-26 Python使用shutil操作文件、subprocess運(yùn)行子程序_python
- 2022-05-06 pyecharts的Tab和Legend布局詳情_python
- 2022-04-17 python使用openpyxl讀取日期并修改excel
- 2023-05-30 模型訓(xùn)練時(shí)GPU利用率太低的原因及解決_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支