title: How terminate child processes when parent process terminated in C#
body: <p><strong>Task:</strong> Auto kill all child processes if parent process terminate. Parent procees can be terminated not only in correct way, but also by killing in ProcessExplorer, for example. 
  How can I do it?</p>
  
  <p>Similar question in <a href="https://stackoverflow.com/questions/2345034/terminate-all-grandchildren-when-terminating-a-child-process">С topic</a> advice to use Job objects. How to use it in C# without exporting external DLL?</p>
  
  <hr>
  
  <p>I tried to use Job Objects. But this code doesn't work properly:</p>
  
  <pre><code>  var job = PInvoke.CreateJobObject(null, null);
    var jobli = new PInvoke.JOBOBJECT_BASIC_LIMIT_INFORMATION();
  
    jobli.LimitFlags = PInvoke.LimitFlags.JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE
                     | PInvoke.LimitFlags.JOB_OBJECT_LIMIT_PRIORITY_CLASS
                     | PInvoke.LimitFlags.JOB_OBJECT_LIMIT_JOB_TIME
                     | PInvoke.LimitFlags.JOB_OBJECT_LIMIT_DIE_ON_UNHANDLED_EXCEPTION
                     | PInvoke.LimitFlags.JOB_OBJECT_LIMIT_JOB_MEMORY;
  
    var res = PInvoke.SetInformationJobObject(job, PInvoke.JOBOBJECTINFOCLASS.JobObjectBasicLimitInformation, jobli, 48);
  
    if (!res)
    {
      int b = PInvoke.GetLastError();
      Console.WriteLine("Error " + b);
    }
  
    var Prc = Process.Start(...);
  
    PInvoke.AssignProcessToJobObject(job, Prc.Handle);
  </code></pre>
  
  <p>PInvoke.SetInformationJobObject returns with error. GetLastError returns error 24.
  However, PInvoke.AssignProcessToJobObject works and child process added to Job Queue (I can see it in ProcessExplorer). But, because PInvoke.SetInformationJobObject don't work - spawned process stay alive when I kill parent one.</p>
  
  <p>What do I have incorrect in this code?</p>