/// <summary> /// Kills an external process that specified by identity /// </summary> /// <param name="processID">The integer that presents the identity of a process that to be killed</param> /// <param name="tryToClose">The action to try to close the process before the process be killed</param> /// <param name="onKilled">The action to run when process was killed</param> /// <param name="onError">The action to run when got error</param> public static void Kill(int processID, Action <Process> tryToClose = null, Action <Process> onKilled = null, Action <Exception> onError = null) { try { using (var process = Process.GetProcessById(processID)) { ExternalProcess.Kill(process, tryToClose, onKilled, onError); } } catch (Exception ex) { onError?.Invoke(ex); } }
/// <summary> /// Stops an external process /// </summary> /// <param name="info">The information</param> /// <param name="onCompleted">The action to run when completed</param> /// <param name="onError">The action to run when got error</param> public static void Stop(Info info, Action <Info> onCompleted = null, Action <Exception> onError = null) { if (info == null || info.Process == null || info.Process.HasExited) { try { info?.Process?.Dispose(); onCompleted?.Invoke(info); } catch (Exception ex) { onError?.Invoke(ex); } } else { ExternalProcess.Kill( info.Process, process => { info.Process.StandardInput.WriteLine("exit"); info.Process.StandardInput.Close(); info.Process.WaitForExit(456); info.Process.Refresh(); }, process => { try { info.ExitCode = info.Process.ExitCode; info.ExitTime = info.Process.ExitTime; } catch (Exception ex) { onError?.Invoke(ex); } try { info.Process.Dispose(); } catch (Exception ex) { onError?.Invoke(ex); } onCompleted?.Invoke(info); }, onError ); } }