tags:

views:

30

answers:

1

Hi, I'm writing a program that is able to throw up errors due using LuaInterface and loading in editable scripts, this causes a problem as on error no "Application.Exit" or "Form.FormClosing" events seem to be called. I therefore cannot guarantee the shutdown of a process I run within the application.

Is there any way of closing the launched process when the main process ends via error?

+1  A: 

Unless the process is ended forcefully such as by killing it from the task manager or using Environment.FailFast, you should be able to do something like this in your Main method:

public static void Main(string[] args) {

    try {
        DoNormalStartupStuff();
    }
    finally {
        foreach (var process in _runningProcesses) {
            process.Kill();
        }
    }

}

The other option is to make the child processes aware of their parent process id, either by passing the process id from the parent to the child process or doing something like this. Then the child process can monitor the parent process and if it terminates, the child process can commit suicide.

Josh Einstein