Repository navigation
Expand file tree
/
Copy pathCProcess.cs
More file actions
107 lines (94 loc) · 2.9 KB
/
Copy pathCProcess.cs
File metadata and controls
107 lines (94 loc) · 2.9 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
using System;
using System.IO;
using System.Diagnostics;
namespace RapChessGui
{
public class CProcess
{
private readonly DataReceivedEventHandler dataR = null;
public Process process = null;// new Process();
public CProcess(DataReceivedEventHandler drh)
{
dataR = new DataReceivedEventHandler(drh);
}
public int GetPid()
{
return process?.Id ?? 0;
}
private void OnErrorReceived(object sender, DataReceivedEventArgs e)
{
try
{
if (!String.IsNullOrEmpty(e.Data))
FormChess.log.Add(e.Data);
}
catch { }
}
public int SetProgram(string path, string param = "")
{
Terminate();
if (File.Exists(path))
{
process = new Process();
process.EnableRaisingEvents = true;
process.StartInfo.FileName = path;
process.StartInfo.Arguments = param;
process.StartInfo.WorkingDirectory = Path.GetDirectoryName(path);
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.UseShellExecute = false;
process.OutputDataReceived += dataR;
process.ErrorDataReceived += new DataReceivedEventHandler(OnErrorReceived);
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.PriorityClass = FormOptions.priority;
process.StandardInput.AutoFlush = true;
return process.Id;
}
return 0;
}
public void Restart()
{
if (process != null)
SetProgram(process.StartInfo.FileName, process.StartInfo.Arguments);
}
public void Stop()
{
WriteLine("stop");
}
public void Quit()
{
WriteLine("quit");
}
public void Close()
{
if (process != null)
{
process.OutputDataReceived -= dataR;
Quit();
process = null;
}
}
public void Terminate()
{
try
{
if (process != null)
{
process.OutputDataReceived -= dataR;
process.Kill();
process = null;
}
}
catch { }
}
public void WriteLine(string c, int sleep = 0)
{
process?.StandardInput?.WriteLine(c);
System.Threading.Thread.Sleep(sleep);
}
}
}