-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCProcessBuf.cs
More file actions
150 lines (135 loc) · 3.81 KB
/
CProcessBuf.cs
File metadata and controls
150 lines (135 loc) · 3.81 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Diagnostics;
namespace RapChessGui
{
public class CProcessBuf
{
protected Process process = null;
readonly private object locker = new object();
private readonly List<string> listMsg = new List<string>();
/// <summary>
/// Get memory usage
/// </summary>
public string GetMemory()
{
if (process == null)
return "Memory";
process.Refresh();
if (process.HasExited)
return "Memory";
return process.PrivateMemorySize64.ToString("N0");
}
void SetMessage(string msg)
{
lock (locker)
{
listMsg.Add(msg);
}
}
public List<string> GetMessages()
{
List<string> list = new List<string>();
lock (locker)
{
list.AddRange(listMsg);
listMsg.Clear();
}
return list;
}
public void Clear()
{
lock (locker)
{
listMsg.Clear();
}
}
private void OnDataReceived(object sender, DataReceivedEventArgs e)
{
try
{
if (!String.IsNullOrEmpty(e.Data))
{
string msg = e.Data.Trim();
SetMessage(msg);
}
}
catch { }
}
public bool SetProgram(string path, string param = "")
{
Terminate();
if (File.Exists(path))
{
process = new Process();
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 += new DataReceivedEventHandler(OnDataReceived);
process.Start();
process.BeginOutputReadLine();
process.PriorityClass = FormOptions.priority;
process.StandardInput.AutoFlush = true;
return true;
}
return false;
}
public void Restart()
{
SetProgram(process.StartInfo.FileName, process.StartInfo.Arguments);
}
public void Stop()
{
WriteLine("stop");
}
public void Quit()
{
WriteLine("quit");
}
public void Close()
{
try
{
if (process != null)
{
process.OutputDataReceived -= OnDataReceived;
Clear();
Quit();
process.WaitForExit(100);
if (!process.HasExited)
process.Kill();
process = null;
}
}
catch
{
}
}
public void Terminate()
{
try
{
if (process != null)
{
process.OutputDataReceived -= OnDataReceived;
Clear();
process.Kill();
process = null;
}
}
catch
{
}
}
public void WriteLine(string c)
{
process?.StandardInput.WriteLine(c);
}
}
}