diff --git a/ClientTest/ClientTest.csproj b/ClientTest/ClientTest.csproj index b5254e01..57079caf 100644 --- a/ClientTest/ClientTest.csproj +++ b/ClientTest/ClientTest.csproj @@ -21,7 +21,7 @@ - + diff --git a/Stardust.Server/Stardust.Server.csproj b/Stardust.Server/Stardust.Server.csproj index 4e93aaac..09a21b71 100644 --- a/Stardust.Server/Stardust.Server.csproj +++ b/Stardust.Server/Stardust.Server.csproj @@ -33,7 +33,7 @@ - + diff --git a/Stardust.Web/Stardust.Web.csproj b/Stardust.Web/Stardust.Web.csproj index 53e5c871..b3b5e1c4 100644 --- a/Stardust.Web/Stardust.Web.csproj +++ b/Stardust.Web/Stardust.Web.csproj @@ -43,7 +43,7 @@ - + diff --git a/Stardust/Managers/ServiceController.cs b/Stardust/Managers/ServiceController.cs index 36da11a4..c949122c 100644 --- a/Stardust/Managers/ServiceController.cs +++ b/Stardust/Managers/ServiceController.cs @@ -1,9 +1,11 @@ -using System.Diagnostics; +using System.ComponentModel; +using System.Diagnostics; using NewLife; using NewLife.Log; using NewLife.Threading; using Stardust.Deployment; using Stardust.Models; +using Stardust.Registry; using Stardust.Services; #if !NET40 using TaskEx = System.Threading.Tasks.Task; @@ -387,11 +389,11 @@ public void Stop(String reason) try { - if (!p.HasExited && p.CloseMainWindow()) + if (!p.GetProcessHasExited() && p.CloseMainWindow()) { WriteLog("已发送关闭窗口消息,等待目标进程退出"); - for (var i = 0; i < 50 && !p.HasExited; i++) + for (var i = 0; i < 50 && !p.GetProcessHasExited(); i++) { Thread.Sleep(200); } @@ -400,7 +402,7 @@ public void Stop(String reason) catch { } // 优雅关闭进程 - if (!p.HasExited) + if (!p.GetProcessHasExited()) { try { @@ -409,7 +411,7 @@ public void Stop(String reason) { Process.Start("kill", p.Id.ToString()); - for (var i = 0; i < 50 && !p.HasExited; i++) + for (var i = 0; i < 50 && !p.GetProcessHasExited(); i++) { Thread.Sleep(200); } @@ -418,7 +420,7 @@ public void Stop(String reason) { Process.Start("taskkill", $"-pid {p.Id}"); - for (var i = 0; i < 50 && !p.HasExited; i++) + for (var i = 0; i < 50 && !p.GetProcessHasExited(); i++) { Thread.Sleep(200); } @@ -429,13 +431,13 @@ public void Stop(String reason) try { - if (!p.HasExited) + if (!p.GetProcessHasExited()) { WriteLog("强行结束进程 PID={0}/{1}", p.Id, p.ProcessName); p.Kill(); } - if (p.HasExited) WriteLog("进程[PID={0}]已退出!ExitCode={1}", p.Id, p.ExitCode); + if (p.GetProcessHasExited()) WriteLog("进程[PID={0}]已退出!ExitCode={1}", p.Id, p.ExitCode); } catch (Exception ex) { @@ -472,7 +474,7 @@ public Boolean Check() span?.AppendTag("CheckMaxMemory"); try { - if (!p.HasExited) + if (!p.GetProcessHasExited()) { _error = 0; @@ -515,7 +517,7 @@ public Boolean Check() var exited = false; try { - exited = p.HasExited; + exited = p.GetProcessHasExited(); } catch { } @@ -556,7 +558,7 @@ public Boolean Check() // 遍历所有进程,从命令行参数中找到启动文件名一致的进程 foreach (var item in Process.GetProcesses()) { - if (item.Id == mypid || item.HasExited) continue; + if (item.Id == mypid || item.GetProcessHasExited()) continue; if (!item.ProcessName.EqualIgnoreCase(ProcessName)) continue; var name = AppInfo.GetProcessName(item); @@ -575,7 +577,7 @@ public Boolean Check() { span?.AppendTag($"GetProcessesByName({ProcessName})"); - var ps = Process.GetProcessesByName(ProcessName).Where(e => e.Id != mypid && !e.HasExited).ToArray(); + var ps = Process.GetProcessesByName(ProcessName).Where(e => e.Id != mypid && !e.GetProcessHasExited()).ToArray(); if (ps.Length > 0) return TakeOver(ps[0], $"按[Name={ProcessName}]查找"); } } diff --git a/Stardust/StarHelper.cs b/Stardust/StarHelper.cs index 5f21a669..e54a35cd 100644 --- a/Stardust/StarHelper.cs +++ b/Stardust/StarHelper.cs @@ -1,4 +1,5 @@ -using System.Diagnostics; +using System.ComponentModel; +using System.Diagnostics; using NewLife; using NewLife.Caching; using NewLife.Configuration; @@ -54,7 +55,7 @@ public static StarFactory AddStardust(this IObjectContainer services, String? se /// public static Process SafetyKill(this Process process) { - if (process == null || process.HasExited) return process; + if (process == null || process.GetProcessHasExited()) return process; try { @@ -62,7 +63,7 @@ public static Process SafetyKill(this Process process) { Process.Start("kill", process.Id.ToString()); - for (var i = 0; i < 50 && !process.HasExited; i++) + for (var i = 0; i < 50 && !process.GetProcessHasExited(); i++) { Thread.Sleep(200); } @@ -71,7 +72,7 @@ public static Process SafetyKill(this Process process) { Process.Start("taskkill", $"-pid {process.Id}"); - for (var i = 0; i < 50 && !process.HasExited; i++) + for (var i = 0; i < 50 && !process.GetProcessHasExited(); i++) { Thread.Sleep(200); } @@ -79,8 +80,25 @@ public static Process SafetyKill(this Process process) } catch { } - if (!process.HasExited) process.Kill(); + if (!process.GetProcessHasExited()) process.Kill(); return process; } + + /// 获取进程是否终止 + public static Boolean GetProcessHasExited(this Process process) + { + try + { + return process.HasExited; + } + catch (Win32Exception) + { + return true; + } + //catch + //{ + // return false; + //} + } }