Skip to content

Commit

Permalink
Merge pull request #43 from qcjxberin/master
Browse files Browse the repository at this point in the history
增加获取进程是否终止:Process.HasExited的通用方法,用于解决拒绝访问问题
  • Loading branch information
nnhy authored Nov 2, 2023
2 parents 647bad7 + b91dd09 commit 973b4e5
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 20 deletions.
2 changes: 1 addition & 1 deletion ClientTest/ClientTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="NewLife.Redis" Version="5.5.2023.1001" />
<PackageReference Include="NewLife.Redis" Version="5.5.2023.1102" />
<PackageReference Include="NewLife.UnitTest" Version="1.0.2023.905" />
<PackageReference Include="xunit" Version="2.6.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3">
Expand Down
2 changes: 1 addition & 1 deletion Stardust.Server/Stardust.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

<ItemGroup>
<PackageReference Include="NewLife.IP" Version="2.1.2023.1101" />
<PackageReference Include="NewLife.Redis" Version="5.5.2023.1001" />
<PackageReference Include="NewLife.Redis" Version="5.5.2023.1102" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion Stardust.Web/Stardust.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

<ItemGroup>
<PackageReference Include="NewLife.Cube.Core" Version="5.6.2023.1012" />
<PackageReference Include="NewLife.Redis" Version="5.5.2023.1001" />
<PackageReference Include="NewLife.Redis" Version="5.5.2023.1102" />
</ItemGroup>

<ItemGroup>
Expand Down
26 changes: 14 additions & 12 deletions Stardust/Managers/ServiceController.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -400,7 +402,7 @@ public void Stop(String reason)
catch { }

// 优雅关闭进程
if (!p.HasExited)
if (!p.GetProcessHasExited())
{
try
{
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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)
{
Expand Down Expand Up @@ -472,7 +474,7 @@ public Boolean Check()
span?.AppendTag("CheckMaxMemory");
try
{
if (!p.HasExited)
if (!p.GetProcessHasExited())
{
_error = 0;

Expand Down Expand Up @@ -515,7 +517,7 @@ public Boolean Check()
var exited = false;
try
{
exited = p.HasExited;
exited = p.GetProcessHasExited();
}
catch { }

Expand Down Expand Up @@ -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);
Expand All @@ -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}]查找");
}
}
Expand Down
28 changes: 23 additions & 5 deletions Stardust/StarHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics;
using System.ComponentModel;
using System.Diagnostics;
using NewLife;
using NewLife.Caching;
using NewLife.Configuration;
Expand Down Expand Up @@ -54,15 +55,15 @@ public static StarFactory AddStardust(this IObjectContainer services, String? se
/// <returns></returns>
public static Process SafetyKill(this Process process)
{
if (process == null || process.HasExited) return process;
if (process == null || process.GetProcessHasExited()) return process;

try
{
if (Runtime.Linux)
{
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);
}
Expand All @@ -71,16 +72,33 @@ 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);
}
}
}
catch { }

if (!process.HasExited) process.Kill();
if (!process.GetProcessHasExited()) process.Kill();

return process;
}

/// <summary>获取进程是否终止</summary>
public static Boolean GetProcessHasExited(this Process process)
{
try
{
return process.HasExited;
}
catch (Win32Exception)
{
return true;
}
//catch
//{
// return false;
//}
}
}

0 comments on commit 973b4e5

Please sign in to comment.