-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathAbstractStrategy.cs
More file actions
64 lines (56 loc) · 2.43 KB
/
Copy pathAbstractStrategy.cs
File metadata and controls
64 lines (56 loc) · 2.43 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
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using GeneralUpdate.Common.FileBasic;
using GeneralUpdate.Common.Shared;
namespace GeneralUpdate.Bowl.Strategys;
internal abstract class AbstractStrategy : IStrategy
{
protected MonitorParameter _parameter;
protected List<string> OutputList = new ();
public void SetParameter(MonitorParameter parameter) => _parameter = parameter;
public virtual void Launch()
{
GeneralTracer.Info($"AbstractStrategy.Launch: starting inner application. App={_parameter.InnerApp}, Args={_parameter.InnerArguments}");
Startup(_parameter.InnerApp, _parameter.InnerArguments);
GeneralTracer.Info("AbstractStrategy.Launch: inner application process finished.");
}
private void Startup(string appName, string arguments)
{
GeneralTracer.Info($"AbstractStrategy.Startup: preparing process. FileName={appName}");
if (Directory.Exists(_parameter.FailDirectory))
{
GeneralTracer.Info($"AbstractStrategy.Startup: removing existing fail directory: {_parameter.FailDirectory}");
StorageManager.DeleteDirectory(_parameter.FailDirectory);
}
Directory.CreateDirectory(_parameter.FailDirectory);
GeneralTracer.Info($"AbstractStrategy.Startup: fail directory created: {_parameter.FailDirectory}");
var startInfo = new ProcessStartInfo
{
FileName = appName,
Arguments = arguments,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
};
var process = new Process { StartInfo = startInfo };
process.OutputDataReceived += OutputHandler;
process.ErrorDataReceived += OutputHandler;
process.Start();
GeneralTracer.Info($"AbstractStrategy.Startup: process started. PID={process.Id}");
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit(1000 * 10);
GeneralTracer.Info($"AbstractStrategy.Startup: process exited. ExitCode={process.ExitCode}");
}
private void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
var data = outLine.Data;
if (!string.IsNullOrEmpty(data))
{
GeneralTracer.Debug($"AbstractStrategy.OutputHandler: {data}");
OutputList.Add(data);
}
}
}