There are several different options. I would suggest that you have an application that tracks the processes it runs:
private Stack<Process> _startedProcesses = new Stack<Process>(); private void StartChildProcess(string fileName) { Process newProcess = new Process(); newProcess.StartInfo = new ProcessStartInfo(fileName); ; newProcess.Start(); _startedProcesses.Push(newProcess); }
When the application closes, you can call a method that closes all running child processes that are still running. You can use this either with the Kill method, or by calling the CloseMainWindow and Close methods. CloseMainWindow / Close will perform a more elegant closure (if you run Notepad and there are unsaved changes, Kill will lose them, CloseMainWindow / Close will make a notepad if you want to save):
private void CloseStartedProcesses() { while (_startedProcesses.Count > 0) { Process process = _startedProcesses.Pop(); if (process != null && !process.HasExited) { process.CloseMainWindow(); process.Close(); } } }
source share