C#如何實(shí)現(xiàn)子進(jìn)程跟隨主進(jìn)程關(guān)閉
前言
多進(jìn)程開發(fā)經(jīng)常會(huì)遇到主進(jìn)程關(guān)閉,子進(jìn)程需要跟隨主進(jìn)程一同關(guān)閉。比如調(diào)ffmpeg命令行實(shí)現(xiàn)的錄屏程序,錄屏程序關(guān)閉,ffmpeg進(jìn)程也需要退出。我們通常在程序關(guān)閉時(shí)調(diào)用Process.Kill()殺掉fmpeg進(jìn)程即可。但是如果是強(qiáng)制或異常關(guān)閉錄屏程序,ffmpeg將會(huì)變成僵尸進(jìn)程殘留在系統(tǒng)中。本文將提供一種解決此類問(wèn)題的方法。
一、如何實(shí)現(xiàn)
1、創(chuàng)建作業(yè)對(duì)象
(1)、創(chuàng)建對(duì)象
handle = CreateJobObject(IntPtr.Zero, null);
(2)、設(shè)置銷毀作業(yè)時(shí),關(guān)閉擁有的進(jìn)程
var info = new JOBOBJECT_BASIC_LIMIT_INFORMATION { LimitFlags = 0x2000 }; var extendedInfo = new JOBOBJECT_EXTENDED_LIMIT_INFORMATION { BasicLimitInformation = info }; int length = Marshal.SizeOf(typeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION)); IntPtr extendedInfoPtr = Marshal.AllocHGlobal(length); Marshal.StructureToPtr(extendedInfo, extendedInfoPtr, false); if (!SetInformationJobObject(handle, JobObjectInfoType.ExtendedLimitInformation, extendedInfoPtr, (uint)length)) throw new Exception(string.Format("Unable to set information. Error: {0}", Marshal.GetLastWin32Error()));
2、子進(jìn)程加入作業(yè)對(duì)象
AssignProcessToJobObject(handle, processHandle);
3、銷毀作業(yè)對(duì)象
(1)、手動(dòng)銷毀
CloseHandle(handle);
(2)、所在進(jìn)程結(jié)束自動(dòng)銷毀
二、完整代碼
using System.Diagnostics; using System.Runtime.InteropServices; namespace JobManagement { #region Helper classes /// <summary> /// 作業(yè)對(duì)象,主要用于子進(jìn)程管理。 /// 目前版本是只支持作業(yè)銷毀擁有的子進(jìn)程退出 /// 通??梢远x一個(gè)全局靜態(tài)變量使用 /// </summary> public class Job : IDisposable { [DllImport("kernel32.dll", CharSet = CharSet.Unicode)] static extern IntPtr CreateJobObject(IntPtr a, string lpName); [DllImport("kernel32.dll")] static extern bool SetInformationJobObject(IntPtr hJob, JobObjectInfoType infoType, IntPtr lpJobObjectInfo, UInt32 cbJobObjectInfoLength); [DllImport("kernel32.dll", SetLastError = true)] static extern bool AssignProcessToJobObject(IntPtr job, IntPtr process); [DllImport("kernel32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] static extern bool CloseHandle(IntPtr hObject); private IntPtr handle; private bool disposed; public Job() { handle = CreateJobObject(IntPtr.Zero, null); var info = new JOBOBJECT_BASIC_LIMIT_INFORMATION { LimitFlags = 0x2000 }; var extendedInfo = new JOBOBJECT_EXTENDED_LIMIT_INFORMATION { BasicLimitInformation = info }; int length = Marshal.SizeOf(typeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION)); IntPtr extendedInfoPtr = Marshal.AllocHGlobal(length); Marshal.StructureToPtr(extendedInfo, extendedInfoPtr, false); if (!SetInformationJobObject(handle, JobObjectInfoType.ExtendedLimitInformation, extendedInfoPtr, (uint)length)) throw new Exception(string.Format("Unable to set information. Error: {0}", Marshal.GetLastWin32Error())); } /// <summary> /// 進(jìn)程加入到作業(yè)對(duì)象中 /// </summary> /// <param name="processHandle">進(jìn)程句柄</param> /// <returns></returns> public bool AddProcess(IntPtr processHandle) { return AssignProcessToJobObject(handle, processHandle); } /// <summary> /// 進(jìn)程加入到作業(yè)對(duì)象中 /// </summary> /// <param name="processId">進(jìn)程Id</param> /// <returns></returns> public bool AddProcess(int processId) { return AddProcess(Process.GetProcessById(processId).Handle); } /// <summary> /// 銷毀作業(yè)對(duì)象,手動(dòng)調(diào)用則其擁有的所有進(jìn)程都會(huì)退出 /// </summary> public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } /// <summary> /// 銷毀作業(yè)對(duì)象,手動(dòng)調(diào)用則其擁有的所有進(jìn)程都會(huì)退出 /// </summary> public void Close() { CloseHandle(handle); handle = IntPtr.Zero; } private void Dispose(bool disposing) { if (disposed) return; if (disposing) { } Close(); disposed = true; } } [StructLayout(LayoutKind.Sequential)] struct IO_COUNTERS { public UInt64 ReadOperationCount; public UInt64 WriteOperationCount; public UInt64 OtherOperationCount; public UInt64 ReadTransferCount; public UInt64 WriteTransferCount; public UInt64 OtherTransferCount; } [StructLayout(LayoutKind.Sequential)] struct JOBOBJECT_BASIC_LIMIT_INFORMATION { public Int64 PerProcessUserTimeLimit; public Int64 PerJobUserTimeLimit; public UInt32 LimitFlags; public UIntPtr MinimumWorkingSetSize; public UIntPtr MaximumWorkingSetSize; public UInt32 ActiveProcessLimit; public UIntPtr Affinity; public UInt32 PriorityClass; public UInt32 SchedulingClass; } [StructLayout(LayoutKind.Sequential)] public struct SECURITY_ATTRIBUTES { public UInt32 nLength; public IntPtr lpSecurityDescriptor; public Int32 bInheritHandle; } [StructLayout(LayoutKind.Sequential)] struct JOBOBJECT_EXTENDED_LIMIT_INFORMATION { public JOBOBJECT_BASIC_LIMIT_INFORMATION BasicLimitInformation; public IO_COUNTERS IoInfo; public UIntPtr ProcessMemoryLimit; public UIntPtr JobMemoryLimit; public UIntPtr PeakProcessMemoryUsed; public UIntPtr PeakJobMemoryUsed; } public enum JobObjectInfoType { AssociateCompletionPortInformation = 7, BasicLimitInformation = 2, BasicUIRestrictions = 4, EndOfJobTimeInformation = 6, ExtendedLimitInformation = 9, SecurityLimitInformation = 5, GroupInformation = 11 } #endregion }
三、使用示例
1、正常退出自動(dòng)結(jié)束子進(jìn)程
.net8.0
using System.Diagnostics; using JobManagement; //創(chuàng)建作業(yè)對(duì)象 Job _job = new Job(); //打開記事本程序 var ps = new Process(); ps.StartInfo.FileName = "notepad.exe"; ps.Start(); //記事本程序進(jìn)程加入到作業(yè)對(duì)象 _job.AddProcess(ps.Handle); //等待3秒后退出程序,記事本程序會(huì)自動(dòng)關(guān)閉 Thread.Sleep(3000);
效果預(yù)覽
2、異常退出自動(dòng)結(jié)束子進(jìn)程
.net8.0
using System.Diagnostics; using JobManagement; //創(chuàng)建作業(yè)對(duì)象 Job _job = new Job(); //打開記事本程序 var ps = new Process(); ps.StartInfo.FileName = "notepad.exe"; ps.Start(); //記事本程序進(jìn)程加入到作業(yè)對(duì)象 _job.AddProcess(ps.Handle); while(true)Thread.Sleep(3000);
效果預(yù)覽
總結(jié)
本文講述的內(nèi)容是windows多進(jìn)程開發(fā)中比較重要的技術(shù),因?yàn)榇蟛糠謭?chǎng)景主進(jìn)程退出后子進(jìn)程應(yīng)該跟隨退出,正常流程中通過(guò)代碼可以在退出時(shí)關(guān)閉所有子進(jìn)程,但是異常崩潰時(shí)則不行,會(huì)出現(xiàn)遺留子進(jìn)程。而本文的方法就很好的解決的這個(gè)問(wèn)題,而且也不需要編寫任何關(guān)閉子進(jìn)程的相關(guān)代碼,方便且省心。
以上就是C#如何實(shí)現(xiàn)子進(jìn)程跟隨主進(jìn)程關(guān)閉的詳細(xì)內(nèi)容,更多關(guān)于C#子進(jìn)程跟隨主進(jìn)程關(guān)閉的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C# 全角和半角轉(zhuǎn)換以及判斷的簡(jiǎn)單代碼
這篇文章介紹了在C#中判斷和轉(zhuǎn)換全角半角的方法,有需要的朋友可以參考一下2013-07-07C# PC版微信消息監(jiān)聽自動(dòng)回復(fù)的實(shí)現(xiàn)方法
這篇文章主要介紹了C# PC版微信消息監(jiān)聽自動(dòng)回復(fù)的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05c# 執(zhí)行動(dòng)態(tài)編譯的方法實(shí)例詳解
在C#中執(zhí)行動(dòng)態(tài)編譯的一種方法是使用 CSharpCodeProvider 類,這篇文章主要介紹了c# 執(zhí)行動(dòng)態(tài)編譯的方法,需要的朋友可以參考下2024-03-03淺談Visual Studio 2019 Vue項(xiàng)目的目錄結(jié)構(gòu)
這篇文章主要介紹了Visual Studio 2019 Vue項(xiàng)目 目錄結(jié)構(gòu),本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03C#結(jié)合JavaScript實(shí)現(xiàn)上傳視頻到騰訊云點(diǎn)播平臺(tái)的操作方法
這篇文章主要介紹了C#結(jié)合JavaScript實(shí)現(xiàn)上傳視頻到騰訊云點(diǎn)播平臺(tái),上傳視頻功能,主要要解決兩個(gè)問(wèn)題,一是在服務(wù)端通過(guò)C#生成簽名和SDKID,二是在客戶端通過(guò)JavaScript上傳視頻到騰訊云點(diǎn)播服務(wù)器,感興趣的朋友跟隨小編一起看看吧2023-11-11C#實(shí)現(xiàn)字符串倒序遍歷的方法小結(jié)
這篇文章主要為大家詳細(xì)介紹了C#中實(shí)現(xiàn)字符串倒序遍歷的常見方法,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,有需要的小伙伴可以參考下2024-02-02