欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#如何實(shí)現(xiàn)子進(jìn)程跟隨主進(jìn)程關(guān)閉

 更新時(shí)間:2024年04月09日 11:17:35   作者:CodeOfCC  
多進(jìn)程開發(fā)經(jīng)常會(huì)遇到主進(jìn)程關(guān)閉,子進(jìn)程需要跟隨主進(jìn)程一同關(guān)閉,比如調(diào)ffmpeg命令行實(shí)現(xiàn)的錄屏程序等,下面我們就來(lái)看看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)文章

最新評(píng)論