SoFunction
Updated on 2025-03-07

C# ends the process and child processes

This is a problem I'm having when calling a batch file in C#. First, I call a batch file through a method, and a lot of programs are called in that batch file. When I exit the C# program, I ended the process of killing the batch file in the program. However, the child process called by the batch cannot be killed with the process of the batch file like directly calling the batch file, but it automatically upgraded to become an independent process.

I checked it online and could query the information of the child process through the NtQueryInformationProcess function, and also found a C# code that killed the process and all child processes. Friends who need it can refer to it.

    static class ProcessExtend
    {
        // [StructLayout()]
        private struct ProcessBasicInformation
        {
            public int ExitStatus;
            public int PebBaseAddress;
            public int AffinityMask;
            public int BasePriority;
            public uint UniqueProcessId;
            public uint InheritedFromUniqueProcessId;
        }

        [DllImport("")]
        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 = ();
            foreach (var p in processes)
            {
                var pbi = new ProcessBasicInformation();
                try
                {
                    uint bytesWritten;
                    if (NtQueryInformationProcess(, 0, ref pbi, (uint)(pbi), out bytesWritten) == 0) // == 0 is OK
                        if ( == )
                            using (var newParent = ((int)))
                                ();
                }
                catch { }
            }
            ();
        }
    }

PS: Today I found that the NtQueryInformationProcess function was invalid on x64-bit programs. The specific reason was unknown. Google did not find the answer. Instead, it found another solution, which was implemented through WMI. It can be used under both x86 and x64.

static void KillProcessAndChildren(int pid)
{
    ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * From Win32_Process Where ParentProcessID=" + pid);
    ManagementObjectCollection moc = ();
    foreach (ManagementObject mo in moc)
    {
        KillProcessAndChildren(Convert.ToInt32(mo["ProcessID"]));
    }
    try
    {
        Process proc = (pid);
        (pid);
        ();
    }
    catch (ArgumentException)
    { 
        /* process already exited */
    }
}

This is all about this article about C# ending process and child processes. I hope it will be helpful to everyone's learning and I hope everyone will support me more.