From ab334d4cd9c84ce05e8866caa13af9269cf9b197 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E7=9F=B3=E5=A4=B4?= Date: Sun, 19 May 2024 11:36:22 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=95=B4=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Command/ShowStatusCommandHandler.cs | 4 --- NewLife.Agent/Menu.cs | 13 ++++++--- NewLife.Agent/Procd.cs | 27 ------------------- NewLife.Agent/ServiceBase.cs | 26 ++++++++---------- 4 files changed, 20 insertions(+), 50 deletions(-) diff --git a/NewLife.Agent/Command/ShowStatusCommandHandler.cs b/NewLife.Agent/Command/ShowStatusCommandHandler.cs index 7aa20d4..e452cd3 100644 --- a/NewLife.Agent/Command/ShowStatusCommandHandler.cs +++ b/NewLife.Agent/Command/ShowStatusCommandHandler.cs @@ -34,10 +34,6 @@ public override Boolean IsShowMenu() /// public override void Process(String[] args) - { - ShowStatus(); - } - public virtual void ShowStatus() { Console.WriteLine(); var color = Console.ForegroundColor; diff --git a/NewLife.Agent/Menu.cs b/NewLife.Agent/Menu.cs index 1a1462a..e07858f 100644 --- a/NewLife.Agent/Menu.cs +++ b/NewLife.Agent/Menu.cs @@ -12,7 +12,7 @@ public class Menu : IComparable public String Name { get; set; } /// 命令 - public string Cmd { get; set; } + public String Cmd { get; set; } /// /// 实例化 @@ -20,19 +20,24 @@ public class Menu : IComparable /// /// /// - public Menu(Char key, String name, string cmd) + public Menu(Char key, String name, String cmd) { Key = key; Name = name; Cmd = cmd; } + /// 比较 + /// + /// public Int32 CompareTo(Menu other) { if (ReferenceEquals(this, other)) return 0; - if (ReferenceEquals(null, other)) return 1; + if (other is null) return 1; + var keyComparison = Key.CompareTo(other.Key); if (keyComparison != 0) return keyComparison; - return String.Compare(Cmd, other.Cmd, StringComparison.Ordinal); + + return String.Compare(Cmd, other.Cmd, StringComparison.Ordinal); } } \ No newline at end of file diff --git a/NewLife.Agent/Procd.cs b/NewLife.Agent/Procd.cs index 5851836..209caf8 100644 --- a/NewLife.Agent/Procd.cs +++ b/NewLife.Agent/Procd.cs @@ -287,31 +287,4 @@ public override Boolean Restart(String serviceName) return true; } - - static Process GetProcessById(Int32 processId) - { - try - { - return Process.GetProcessById(processId); - } - catch { } - - return null; - } - - static Boolean GetHasExited(Process process) - { - try - { - return process.HasExited; - } - catch (Win32Exception) - { - return true; - } - //catch - //{ - // return false; - //} - } } \ No newline at end of file diff --git a/NewLife.Agent/ServiceBase.cs b/NewLife.Agent/ServiceBase.cs index dc63f3b..bebca8b 100644 --- a/NewLife.Agent/ServiceBase.cs +++ b/NewLife.Agent/ServiceBase.cs @@ -1,5 +1,4 @@ using System.Diagnostics; -using System.Reflection; using System.Runtime; using System.Runtime.CompilerServices; using System.Security; @@ -30,6 +29,9 @@ public abstract class ServiceBase : DisposeBase /// 是否使用自启动。自启动需要用户登录桌面,默认false使用系统服务 public Boolean UseAutorun { get; set; } + + /// 运行中 + public Boolean Running { get; set; } #endregion /// @@ -195,9 +197,9 @@ protected virtual void ProcessMenu() try { Command.Handle(key.KeyChar, args); - } - catch (Exception ex) - { + } + catch (Exception ex) + { XTrace.WriteException(ex); } Console.WriteLine(); @@ -218,24 +220,18 @@ protected virtual void ShowMenu() foreach (var menu in menus) { Console.WriteLine($" {menu.Key}、 {menu.Name}\t{menu.Cmd}"); - } + } Console.WriteLine($" 0、 退出\t"); Console.WriteLine(); Console.ForegroundColor = color; } - #endregion - /// - #region 服务控制 - /// 显示自定义菜单 - /// - /// - public Boolean Running { get; set; } - /// 添加菜单 + #region 服务控制 private AutoResetEvent _event; private Process _process; + /// 主循环 internal void DoLoop() { @@ -475,8 +471,8 @@ protected virtual Boolean CheckAutoRestart() if (ts.TotalMinutes < auto) return false; var timeRange = Setting.Current.RestartTimeRange?.Split('-'); - if (timeRange?.Length == 2 - && TimeSpan.TryParse(timeRange[0], out var startTime) && startTime <= DateTime.Now.TimeOfDay + if (timeRange?.Length == 2 + && TimeSpan.TryParse(timeRange[0], out var startTime) && startTime <= DateTime.Now.TimeOfDay && TimeSpan.TryParse(timeRange[1], out var endTime) && endTime >= DateTime.Now.TimeOfDay) { WriteLog("服务已运行 {0:n0}分钟,达到预设重启时间({1:n0}分钟),并且当前时间在预设时间范围之内({2}),准备重启!", ts.TotalMinutes, auto, Setting.Current.RestartTimeRange);