One of my app's thread is responsible for running multiple processes in form of bash commands. It runs in a loop and for each found file runs specified command.
foreach (var file in files)
{
string withoutExt = Path.GetFileNameWithoutExtension(file);
string outputFile = Path.GetDirectoryName(file) + "/" + withoutExt + ".png";
tasks.Add(Task.Run(delegate
{
ProcessStartInfo startInfo = new ProcessStartInfo()
{ FileName = "neato", Arguments = $"-Tpng {file} -o {outputFile}", };
Process.Start(startInfo)?.WaitForExit();
}));
}
Task.WaitAll(tasks.ToArray());
Although it does its job, I noticed that those processes are alive after my app is either forcibly closed or when it crashes. I found it out when I've ran this code on too many files: hundreds of processes appeared eating all my resources and when I've finally managed to close an app, they still persisted.
I'm looking for a way to somehow bound those processes to main thread, so they all be cleaned as soon as I close the main thread.
I'm aware that there are already questions asked about such problem on StackOverflow, but as far as I can see, solutions presented there are strictly bound to Windows OS, not Linux.