-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMonitor.cs
84 lines (70 loc) · 1.7 KB
/
Monitor.cs
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System.Diagnostics;
using System.Diagnostics.SymbolStore;
using System.Reflection.Metadata.Ecma335;
namespace Mona;
public class Monitor
{
public const int BAD_PID = -1;
private Process _process;
public Monitor(int pid)
{
_process = Process.GetProcessById(pid);
}
public Monitor(Process process)
{
_process = process;
}
public bool IsValid()
{
if (_process == null) return false;
if (Program.Settings.CheckResponding && !_process.Responding)
{
return false;
}
if (Program.Settings.CheckHasExited && _process.HasExited)
{
return false;
}
return true;
}
public void Refresh()
{
if (_process == null) return;
_process.Refresh();
}
public void Kill()
{
// _process.CloseMainWindow();
// _process.Close();
// Thread.Sleep(500);
_process.Kill(true);
Thread.Sleep(500);
}
public static int GetPIDFromFile()
{
Log.Message($"Attempt to get PID from {Program.Settings.ProcessInfoPath} ...");
int pid = BAD_PID;
// Look for ProcessInfoPath
if (File.Exists(Program.Settings.ProcessInfoPath) && int.TryParse(File.ReadAllText(Program.Settings.ProcessInfoPath).Trim(), out pid))
{
Log.Message($"PID found: {pid}");
}
else
{
Log.Message("PID not found.");
}
return pid;
}
public static bool IsValidPID(int pid)
{
try
{
Process.GetProcessById(pid);
}
catch
{
return false;
}
return true;
}
}