-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcessWatcherCore.cs
213 lines (179 loc) · 5.55 KB
/
ProcessWatcherCore.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
namespace ProcessWatcher
{
using System;
using System.Diagnostics;
using System.Timers;
/// <summary>
/// The core class for watching the process.
/// </summary>
public class ProcessWatcherCore : IDisposable
{
#region Fields
/// <summary>
/// The current process.
/// </summary>
private Process process;
/// <summary>
/// The close timer.
/// </summary>
private Timer closeTimer;
/// <summary>
/// Whether the process has ever been started.
/// </summary>
private bool started;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="ProcessWatcherCore"/> class.
/// </summary>
/// <param name="path">The process path.</param>
/// <param name="arguments">The process arguments.</param>
public ProcessWatcherCore(string path, string arguments)
{
this.process = new Process();
this.process.EnableRaisingEvents = true;
this.process.StartInfo = new ProcessStartInfo(path, arguments);
this.process.Exited += this.OnProcessExited;
this.closeTimer = new Timer(5000);
this.closeTimer.Elapsed += this.OnCloseTimerElapsed;
this.closeTimer.AutoReset = false;
}
#endregion
#region Events
/// <summary>
/// Raised when the process exits.
/// </summary>
public event EventHandler Exited;
#endregion
#region Public Properties
/// <summary>
/// Gets or sets the logger.
/// </summary>
public ILogger Logger
{
get;
set;
}
/// <summary>
/// Gets a value indicating whether the process is currently running.
/// </summary>
public bool IsRunning
{
get
{
return !this.process.HasExited;
}
}
#endregion
#region Public Methods
/// <summary>
/// Launches the application.
/// </summary>
public void Launch()
{
if (!this.started || this.process.HasExited)
{
this.started = true;
this.Log("The process is starting...");
this.process.Start();
}
else
{
this.Log("The process is already running.");
}
}
/// <summary>
/// Closes the application.
/// </summary>
/// <param name="timeout">The timeout in seconds.</param>
public void Close(int timeout)
{
if (!this.process.HasExited)
{
this.Log("The process is closing...");
this.closeTimer.Interval = timeout * 1000;
this.closeTimer.Start();
this.process.CloseMainWindow();
}
else
{
this.Log("The process is not running.");
}
}
/// <summary>
/// Kills the process.
/// </summary>
public void Kill()
{
if (!this.process.HasExited)
{
this.Log("The prociess is being killed...");
this.process.Kill();
}
}
/// <summary>
/// Dispose the process watcher.
/// </summary>
public void Dispose()
{
if (this.closeTimer != null)
{
this.closeTimer.Dispose();
this.closeTimer = null;
}
if (this.process != null)
{
this.process.Dispose();
this.process = null;
}
}
#endregion
#region Private Methods
/// <summary>
/// Log a message.
/// </summary>
/// <param name="message">The log message text.</param>
private void Log(string message)
{
if (this.Logger != null)
{
this.Logger.Log(message);
}
}
/// <summary>
/// Invokes the <see cref="ProcessWatcherCore.Exited"/> event.
/// </summary>
private void OnExited()
{
this.Log("The process has exited.");
if (this.Exited != null)
{
this.Exited.Invoke(this, new EventArgs());
}
}
#endregion
#region Private Event Handlers
/// <summary>
/// Handles the process exited event.
/// </summary>
/// <param name="sender">The sender object.</param>
/// <param name="e">The event arguments.</param>
private void OnProcessExited(object sender, EventArgs e)
{
this.closeTimer.Stop();
this.OnExited();
}
/// <summary>
/// Handles the close timer event.
/// </summary>
/// <param name="sender">The sender object.</param>
/// <param name="e">The event arguments.</param>
private void OnCloseTimerElapsed(object sender, ElapsedEventArgs e)
{
this.Log("The process failed to close within the specified interval.");
this.closeTimer.Stop();
this.Kill();
}
#endregion
}
}