-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessCommands.cs
More file actions
220 lines (196 loc) · 6.99 KB
/
ProcessCommands.cs
File metadata and controls
220 lines (196 loc) · 6.99 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
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
214
215
216
217
218
219
220
//-------------------------------------------------------------------
// Copyright © 2012 Kindel Systems, LLC
// http://www.kindel.com
// charlie@kindel.com
//
// Published under the MIT License.
// Source control on SourceForge
// http://sourceforge.net/projects/mcecontroller/
//-------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Threading;
using System.Xml.Serialization;
namespace MCEControl
{
/// <summary>
/// Summary description for StartProcessCommands.
/// </summary>
public class StartProcessCommand : Command
{
[XmlAttribute("File")]
public string File;
[XmlAttribute("Arguments")]
public string Arguments;
[XmlAttribute("StopCmd")]
public string StopCommand;
[XmlAttribute("StopFile")]
public string StopFile;
[XmlAttribute("StopArguments")]
public string StopArguments;
[DefaultValue(true)]
[XmlAttribute("LogErrors")]
public bool LogErrors;
[XmlElement("StartProcess", typeof(StartProcessCommand))]
[XmlElement("SendInput", typeof(SendInputCommand))]
[XmlElement("SendMessage", typeof(SendMessageCommand))]
[XmlElement(typeof(Command))]
public Command NextCommand;
[XmlIgnore]
private Executor executor;
public override void Execute(Reply reply)
{
Stop(reply);
if (File != null)
{
executor = new Executor(File, Arguments, LogErrors);
if (!string.IsNullOrWhiteSpace(StopFile))
{
executor.StopExecutor = new Executor(StopFile, StopArguments, LogErrors);
}
else if (!string.IsNullOrWhiteSpace(StopArguments))
{
executor.StopExecutor = new Executor(File, StopArguments, LogErrors);
}
executor.Run();
if (NextCommand != null)
executor.WaitForInputIdle(TimeSpan.FromSeconds(10)); // TODO: Make this settable
}
if (NextCommand != null)
NextCommand.Execute(reply);
}
public void Stop(Reply reply)
{
if (executor != null)
{
executor.Stop();
executor = null;
}
}
public override IEnumerable<Command> AutoCommands()
{
if (!string.IsNullOrWhiteSpace(StopCommand))
{
yield return new StopProcessCommand(this);
}
}
private class Executor
{
public Executor StopExecutor = null;
public Executor(string File, string Arguments, bool logErrors)
{
this.logErrors = logErrors;
var startInfo = new ProcessStartInfo
{
FileName = File,
Arguments = Arguments,
WorkingDirectory = new System.IO.FileInfo(File).DirectoryName,
};
if (logErrors)
{
startInfo.UseShellExecute = true;
startInfo.CreateNoWindow = true;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
}
process = new Process { StartInfo = startInfo };
if (logErrors)
{
process.OutputDataReceived += OnOutputReceived;
process.ErrorDataReceived += OnErrorReceived;
process.EnableRaisingEvents = true;
}
}
public void Run()
{
new Thread(() =>
{
process.Start();
Log($"Started process \"{process.StartInfo.FileName}\" with arguments \"{process.StartInfo.Arguments}\"");
if (logErrors)
{
process.BeginOutputReadLine();
process.BeginErrorReadLine();
}
process.WaitForExit();
Log($"Process exited with code {process.ExitCode} (0x{process.ExitCode:X})");
}).Start();
}
public void Stop()
{
bool exited = false;
try
{
if (StopExecutor != null)
{
StopExecutor.Run();
}
else
{
process.CloseMainWindow();
}
exited = WaitForExit(ExitTimeout);
}
catch (Exception)
{
// Ignore
}
if (!exited)
{
try
{
process.Kill();
}
catch (Exception)
{
// Ignore
}
}
if (StopExecutor != null)
{
StopExecutor.Stop();
}
}
public bool WaitForExit(TimeSpan timeSpan)
{
return process.WaitForExit((int)timeSpan.TotalMilliseconds);
}
public void WaitForInputIdle(TimeSpan timeSpan)
{
process.WaitForInputIdle((int)timeSpan.TotalMilliseconds);
}
private Process process;
private bool logErrors;
private TimeSpan ExitTimeout { get { return TimeSpan.FromSeconds(5); } }
private void Log(string message)
{
MainWindow.AddLogEntry($"[{process.Id:D5}] {message}");
}
private void OnOutputReceived(object sender, DataReceivedEventArgs e)
{
if (e == null || e.Data == null) { return; }
Log($"OUT: {e.Data}");
}
private void OnErrorReceived(object sender, DataReceivedEventArgs e)
{
if (e == null || e.Data == null) { return; }
Log($"ERR: {e.Data}");
}
}
}
public class StopProcessCommand : Command
{
private StartProcessCommand startProcessCommand;
public StopProcessCommand(StartProcessCommand startProcessCommand)
{
Key = startProcessCommand.StopCommand;
this.startProcessCommand = startProcessCommand;
}
public override void Execute(Reply reply)
{
startProcessCommand.Stop(reply);
}
}
}