C#如何實現(xiàn)子進(jìn)程跟隨主進(jìn)程關(guān)閉
前言
多進(jìn)程開發(fā)經(jīng)常會遇到主進(jìn)程關(guān)閉,子進(jìn)程需要跟隨主進(jìn)程一同關(guān)閉。比如調(diào)ffmpeg命令行實現(xiàn)的錄屏程序,錄屏程序關(guān)閉,ffmpeg進(jìn)程也需要退出。我們通常在程序關(guān)閉時調(diào)用Process.Kill()殺掉fmpeg進(jìn)程即可。但是如果是強(qiáng)制或異常關(guān)閉錄屏程序,ffmpeg將會變成僵尸進(jìn)程殘留在系統(tǒng)中。本文將提供一種解決此類問題的方法。
一、如何實現(xiàn)
1、創(chuàng)建作業(yè)對象
(1)、創(chuàng)建對象
handle = CreateJobObject(IntPtr.Zero, null);
(2)、設(shè)置銷毀作業(yè)時,關(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è)對象
AssignProcessToJobObject(handle, processHandle);
3、銷毀作業(yè)對象
(1)、手動銷毀
CloseHandle(handle);
(2)、所在進(jìn)程結(jié)束自動銷毀
二、完整代碼
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace JobManagement
{
#region Helper classes
/// <summary>
/// 作業(yè)對象,主要用于子進(jìn)程管理。
/// 目前版本是只支持作業(yè)銷毀擁有的子進(jìn)程退出
/// 通常可以定義一個全局靜態(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è)對象中
/// </summary>
/// <param name="processHandle">進(jìn)程句柄</param>
/// <returns></returns>
public bool AddProcess(IntPtr processHandle)
{
return AssignProcessToJobObject(handle, processHandle);
}
/// <summary>
/// 進(jìn)程加入到作業(yè)對象中
/// </summary>
/// <param name="processId">進(jìn)程Id</param>
/// <returns></returns>
public bool AddProcess(int processId)
{
return AddProcess(Process.GetProcessById(processId).Handle);
}
/// <summary>
/// 銷毀作業(yè)對象,手動調(diào)用則其擁有的所有進(jìn)程都會退出
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// 銷毀作業(yè)對象,手動調(diào)用則其擁有的所有進(jìn)程都會退出
/// </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、正常退出自動結(jié)束子進(jìn)程
.net8.0
using System.Diagnostics; using JobManagement; //創(chuàng)建作業(yè)對象 Job _job = new Job(); //打開記事本程序 var ps = new Process(); ps.StartInfo.FileName = "notepad.exe"; ps.Start(); //記事本程序進(jìn)程加入到作業(yè)對象 _job.AddProcess(ps.Handle); //等待3秒后退出程序,記事本程序會自動關(guān)閉 Thread.Sleep(3000);
效果預(yù)覽

2、異常退出自動結(jié)束子進(jìn)程
.net8.0
using System.Diagnostics; using JobManagement; //創(chuàng)建作業(yè)對象 Job _job = new Job(); //打開記事本程序 var ps = new Process(); ps.StartInfo.FileName = "notepad.exe"; ps.Start(); //記事本程序進(jìn)程加入到作業(yè)對象 _job.AddProcess(ps.Handle); while(true)Thread.Sleep(3000);
效果預(yù)覽

總結(jié)
本文講述的內(nèi)容是windows多進(jìn)程開發(fā)中比較重要的技術(shù),因為大部分場景主進(jìn)程退出后子進(jìn)程應(yīng)該跟隨退出,正常流程中通過代碼可以在退出時關(guān)閉所有子進(jìn)程,但是異常崩潰時則不行,會出現(xiàn)遺留子進(jìn)程。而本文的方法就很好的解決的這個問題,而且也不需要編寫任何關(guān)閉子進(jìn)程的相關(guān)代碼,方便且省心。
以上就是C#如何實現(xiàn)子進(jìn)程跟隨主進(jìn)程關(guān)閉的詳細(xì)內(nèi)容,更多關(guān)于C#子進(jìn)程跟隨主進(jìn)程關(guān)閉的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C# PC版微信消息監(jiān)聽自動回復(fù)的實現(xiàn)方法
這篇文章主要介紹了C# PC版微信消息監(jiān)聽自動回復(fù)的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
淺談Visual Studio 2019 Vue項目的目錄結(jié)構(gòu)
這篇文章主要介紹了Visual Studio 2019 Vue項目 目錄結(jié)構(gòu),本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03
C#結(jié)合JavaScript實現(xiàn)上傳視頻到騰訊云點(diǎn)播平臺的操作方法
這篇文章主要介紹了C#結(jié)合JavaScript實現(xiàn)上傳視頻到騰訊云點(diǎn)播平臺,上傳視頻功能,主要要解決兩個問題,一是在服務(wù)端通過C#生成簽名和SDKID,二是在客戶端通過JavaScript上傳視頻到騰訊云點(diǎn)播服務(wù)器,感興趣的朋友跟隨小編一起看看吧2023-11-11

