-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessUtils.cs
More file actions
66 lines (62 loc) · 1.98 KB
/
Copy pathProcessUtils.cs
File metadata and controls
66 lines (62 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System;
using System.Diagnostics;
namespace TimeTask
{
/// <summary>
/// 进程树终止工具。
/// .NET Framework 4.7.2 的 Process.Kill() 只终止进程本身:
/// python(pip / torch / funasr)派生出的孙进程会变成孤儿继续占 CPU/显存;
/// 而对仍在运行的进程只 Dispose(例如等待任务被取消时)则什么都不会杀,进程直接脱管。
/// 这里统一用 taskkill /T /F 递归终止整棵进程树,taskkill 不可用时退回普通 Kill。
/// </summary>
internal static class ProcessUtils
{
public static void KillTree(Process process, string reason = null)
{
if (process == null)
{
return;
}
int pid;
string name;
try
{
if (process.HasExited)
{
return;
}
pid = process.Id;
name = process.ProcessName;
}
catch
{
// 进程已退出或句柄失效:无事可做
return;
}
VoiceRuntimeLog.Info(
$"KillTree: pid={pid}, name={name}" + (string.IsNullOrEmpty(reason) ? "" : $", reason={reason}"));
try
{
var psi = new ProcessStartInfo("taskkill", $"/PID {pid} /T /F")
{
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardError = true
};
using (var killer = Process.Start(psi))
{
killer?.WaitForExit(5000);
}
}
catch
{
try { process.Kill(); } catch { }
}
finally
{
try { process.WaitForExit(3000); } catch { }
}
}
}
}