using System; using System.Collections.Generic; using System.Diagnostics; using System.Threading; namespace Netch.Tools { public class Guard { /// /// 启动信息 /// public ProcessStartInfo StartInfo; /// /// 标准模式 /// public bool Standard = true; /// /// 判定启动的字符串 /// public List JudgmentStarted; /// /// 判定停止的字符串 /// public List JudgmentStopped; /// /// 自动重启 /// public bool AutoRestart; /// /// 启动 /// /// public bool Create() { this.instance = new Process { StartInfo = this.StartInfo, EnableRaisingEvents = true }; this.instance.StartInfo.RedirectStandardError = true; this.instance.StartInfo.RedirectStandardOutput = true; this.instance.Exited += this.OnExited; this.instance.ErrorDataReceived += this.OnOutputDataReceived; this.instance.OutputDataReceived += this.OnOutputDataReceived; this.Started = false; this.Starting = true; this.instance.Start(); if (!this.Standard) { this.Started = true; this.instance.BeginErrorReadLine(); this.instance.BeginOutputReadLine(); return true; } this.instance.BeginErrorReadLine(); this.instance.BeginOutputReadLine(); for (var i = 0; i < 1000; i++) { Thread.Sleep(10); if (this.Started) { return true; } if (!this.Starting) { return false; } } this.Delete(); return false; } /// /// 停止 /// public void Delete() { this.AutoRestart = false; try { this.instance?.Kill(); this.instance?.WaitForExit(); } catch (Exception) { // ignore } } /// /// 进程 /// private Process instance; /// /// 是否已启动 /// private bool Started = false; /// /// 是否正在启动中 /// private bool Starting = false; private void OnExited(object sender, EventArgs e) { if (this.Started && this.AutoRestart) { Thread.Sleep(200); this.Create(); } } private void OnOutputDataReceived(object sender, DataReceivedEventArgs e) { if (this.Starting) { if (this.instance.HasExited) { this.Starting = false; } for (int i = 0; i < this.JudgmentStarted.Count; i++) { if (e.Data.ToLower().Contains(this.JudgmentStarted[i])) { this.Started = true; this.Starting = false; return; } } for (int i = 0; i < this.JudgmentStopped.Count; i++) { if (e.Data.ToLower().Contains(this.JudgmentStopped[i])) { this.Starting = false; return; } } } Console.WriteLine($"[Netch][Tools.Guard] {e.Data}"); } } }