2

I'm creating new processes from my application and want these processes to be killed when/if my application is crashed.

So i found this post: Kill child process when parent process is killed. I took the first example and copied all the code to a new class under a function public void Close().

Got an error - The name Win32 does not exist in the current context so I added Microsoft.Win32 and now another error - CloseHandle is not recognized.

The type or namespace name 'CloseHandle' does not exist in the namespace 'Microsoft.Win32' (are you missing an assembly reference?)

How can i fix it ?

Community
  • 1
  • 1
user2214609
  • 4,713
  • 9
  • 34
  • 41
  • http://pinvoke.net/default.aspx/kernel32/CloseHandle.html – Hans Passant Sep 15 '13 at 17:27
  • 1
    Possible duplicate of [Kill child process when parent process is killed](http://stackoverflow.com/questions/3342941/kill-child-process-when-parent-process-is-killed) – Dekay Jun 27 '16 at 13:43

1 Answers1

3

a simpler approach- or so i believe

keep track of all the processes u start.

ArrayList chilProcess_id = new ArrayList();

    public void process_starter()
    {            
        Process p;
        p = new System.Diagnostics.Process();           
        p.StartInfo.FileName = "YOUR PROCESS PATH";
        if (!p.Start())
             chilProcess_id.Add(p.Id);
    }

Now before exiting, kill those processes.

public void exitApplication()
    {
        Process p;
        foreach (int p_id in chilProcess_id)
        {
            p = Process.GetProcessById(p_id);
            try
            {
                if(!p.HasExited)
                    p.Kill();
            }
            catch (Exception e)
            {
                //Handle the exception as you wish
            }
        }
    }

for ArrayList add namespace

using System.Collections;

for Process add namespace

using System.Diagnostics;
rahulroy9202
  • 2,730
  • 3
  • 32
  • 45