7

I'm very new to C# so my question may sound rediculous. I'm developing an application which sometimes need to run ffmpeg. As you guess, this ffmpeg process must be killed when it's host app was closed. I use such code for this task:

AppDomain.CurrentDomain.ProcessExit += new EventHandler(OnProcessExit);
private void OnProcessExit(object sender, EventArgs e)
   {
      proc.Kill();
   }

This works fine, when the app is closed correctly (via it's interface or with Taskman - Applications). The problem is that OnProcessExit event won't trigger, if the program's process was killed (with Taskman - Processes). As far as I know, killing process and closing program actions are not the same on the low level, but I guess, killing process is a command to it and it can be handled with C# tools. So, is it possible, to close child process in this case?

leppie
  • 115,091
  • 17
  • 196
  • 297
JustLogin
  • 1,822
  • 5
  • 28
  • 50
  • 3
    [This question](http://stackoverflow.com/questions/3342941/kill-child-process-when-parent-process-is-killed?rq=1) talks about killing child processes when their parent is killed. – Scott Miller Apr 15 '13 at 13:31
  • Thanks, Scott Miller, [this][1] method is all I need to solve my problem. [1]: http://stackoverflow.com/questions/3342941/kill-child-process-when-parent-process-is-killed?rq=1 – JustLogin Apr 15 '13 at 13:40
  • 3
    No, that's a bad idea. Trying to do something reasonable when the user does something unreasonable is just wasted effort. You have no idea *why* the user decided to kill your program like this. But you typically assume that it isn't because your program is still operating correctly. The odds that whatever you intend to do to kill ffmpeg will come to a good end are accordingly slim. Job objects are not the solution either, they got broken badly in Vista. Now you got two problems. – Hans Passant Apr 15 '13 at 15:39

3 Answers3

1

I think Try this

Application.Exit();
Freelancer
  • 9,008
  • 7
  • 42
  • 81
Kushal Patil
  • 205
  • 1
  • 2
  • 14
0

I recommend to use job objects (as per Scott Miller suggestion). Another option can be have special helper app for your app, which does following:

  • Start your app
  • When your app crashed, clean up after it.

But job objects is definitely better option, it specifically made for this

Leotsarev
  • 1,040
  • 7
  • 23
0

let your host program submit its program ID as parameter, and then listen if the program exits.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    static class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            if (args.Length != 0)
                new System.Threading.Thread( new System.Threading.ParameterizedThreadStart(handelexit)).Start(args[0]);

            // your code here
        }

        static void handelexit(object data)
        {
            int id = System.Convert.ToInt32(data.ToString());

            System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(id);
            while (!p.HasExited)
                System.Threading.Thread.Sleep(100);

            System.Environment.Exit(0);
        }
    }
}
Johan Polson
  • 117
  • 3
  • 12