public void Kill() { if (Process?.HasExited ?? true) { return; } try { var p = System.Diagnostics.Process.GetProcessById(Pid); p.Refresh(); //try to kill the process and retry not more than 10 times if process has not exited var tries = 0; while (!p.HasExited || tries <= 10) { tries++; p.Kill(); } if (!p.HasExited) { WarewolfLogger.Error("Failed to kill process", "ProcessId - " + p.Id); } } catch (Exception ex) { // ignore exception (most likely caused by process already killed) WarewolfLogger.Error(ex.Message, ex.StackTrace); } }
public void Start() { if (IsAlive) { return; } _thread = new Thread(() => { try { var startInfo = GetProcessStartInfo(); startInfo.UseShellExecute = false; startInfo.RedirectStandardOutput = true; startInfo.RedirectStandardError = true; startInfo.RedirectStandardInput = true; using (var process = _processFactory.Start(startInfo)) { _childProcessTracker.Add(process); Process = process; var successTask = Task.Run(async() => { var stream = process.StandardOutput; while (!stream.EndOfStream) { var result = await stream.ReadLineAsync(); WarewolfLogger.Info(result, startInfo.FileName); } }); var errorTask = Task.Run(async() => { var stream = process.StandardError; while (!stream.EndOfStream) { var result = await stream.ReadLineAsync(); WarewolfLogger.Error(result, startInfo.FileName); } }); while (!process.WaitForExit(1000)) { // } Task.WaitAll(successTask, errorTask); OnProcessDied?.Invoke(Config); } } catch { } }) { IsBackground = true }; _thread.Start(); }