public override int WaitForExitAndForward() { AutoResetEvent stop = new AutoResetEvent(false); Console.CancelKeyPress += (s, e) => Catch(() => stop.Set()); System.Runtime.Loader.AssemblyLoadContext.Default.Unloading += (s) => Catch(() => stop.Set()); AppDomain.CurrentDomain.ProcessExit += (s, e) => Catch(() => stop.Set()); bool childStopped = false; if (ChildProcess != null) { new Thread(() => { try { ChildProcess.WaitForExit(); stop.Set(); childStopped = true; } catch { } }).Start(); } stop.WaitOne(); if (childStopped) { return(ChildProcess.ExitCode); } if (ChildProcess != null) { if (!ChildProcess.HasExited) { ChildProcess.Kill(); ChildProcess.WaitForExit(); } } return(0); }