Skip to content

Commit

Permalink
代码整理
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed May 19, 2024
1 parent 57c58e6 commit ab334d4
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 50 deletions.
4 changes: 0 additions & 4 deletions NewLife.Agent/Command/ShowStatusCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ public override Boolean IsShowMenu()

/// <inheritdoc/>
public override void Process(String[] args)
{
ShowStatus();
}
public virtual void ShowStatus()
{
Console.WriteLine();
var color = Console.ForegroundColor;
Expand Down
13 changes: 9 additions & 4 deletions NewLife.Agent/Menu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,32 @@ public class Menu : IComparable<Menu>
public String Name { get; set; }

/// <summary>命令</summary>
public string Cmd { get; set; }
public String Cmd { get; set; }

/// <summary>
/// 实例化
/// </summary>
/// <param name="key"></param>
/// <param name="name"></param>
/// <param name="cmd"></param>
public Menu(Char key, String name, string cmd)
public Menu(Char key, String name, String cmd)
{
Key = key;
Name = name;
Cmd = cmd;
}

/// <summary>比较</summary>
/// <param name="other"></param>
/// <returns></returns>
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);
}
}
27 changes: 0 additions & 27 deletions NewLife.Agent/Procd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
//}
}
}
26 changes: 11 additions & 15 deletions NewLife.Agent/ServiceBase.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Diagnostics;
using System.Reflection;
using System.Runtime;
using System.Runtime.CompilerServices;
using System.Security;
Expand Down Expand Up @@ -30,6 +29,9 @@ public abstract class ServiceBase : DisposeBase

/// <summary>是否使用自启动。自启动需要用户登录桌面,默认false使用系统服务</summary>
public Boolean UseAutorun { get; set; }

/// <summary>运行中</summary>
public Boolean Running { get; set; }
#endregion

/// <summary>
Expand Down Expand Up @@ -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();
Expand All @@ -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
/// <summary>
#region 服务控制
/// 显示自定义菜单
/// </summary>
/// <param name="menus"></param>

public Boolean Running { get; set; }
/// <summary>添加菜单</summary>
#region 服务控制
private AutoResetEvent _event;
private Process _process;

/// <summary>主循环</summary>
internal void DoLoop()
{
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit ab334d4

Please sign in to comment.