root / HServer / 01.Manager / 00.Program / Manager / Process_Cmd.cs
이력 | 보기 | 이력해설 | 다운로드 (5.21 KB)
| 1 |
using System; |
|---|---|
| 2 |
using System.Collections.Generic; |
| 3 |
using System.Diagnostics; |
| 4 |
using System.Linq; |
| 5 |
using System.Text; |
| 6 |
using System.Threading.Tasks; |
| 7 |
using System.Windows.Forms; |
| 8 |
|
| 9 |
namespace Manager |
| 10 |
{
|
| 11 |
public class Process_Cmd |
| 12 |
{
|
| 13 |
#region [ Variable ] |
| 14 |
|
| 15 |
ProcessStartInfo procInfo; |
| 16 |
|
| 17 |
Process process; |
| 18 |
|
| 19 |
public bool isProcessAlive = false; |
| 20 |
|
| 21 |
/// <summary> |
| 22 |
/// 결과를 출력할 리스트 박스 |
| 23 |
/// </summary> |
| 24 |
ListBox resultListBox; |
| 25 |
|
| 26 |
#endregion |
| 27 |
|
| 28 |
public void SetListBox(ListBox inputListbox_) |
| 29 |
{
|
| 30 |
resultListBox = inputListbox_; |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
public Process_Cmd() |
| 35 |
{
|
| 36 |
procInfo = new ProcessStartInfo(); |
| 37 |
} |
| 38 |
|
| 39 |
void CreateProcess() |
| 40 |
{
|
| 41 |
process = new Process(); |
| 42 |
process.StartInfo = procInfo; |
| 43 |
process.EnableRaisingEvents = true; // 이벤트가 발생하게 해준다. |
| 44 |
process.Exited += new EventHandler(Process_Exited); // Process 가 끝날때의 이벤트 |
| 45 |
process.OutputDataReceived += new DataReceivedEventHandler(OutputDataReceive_Handler); |
| 46 |
|
| 47 |
} |
| 48 |
|
| 49 |
/// <summary> |
| 50 |
/// Process 의 정보 값들을 초기화 해줍니다. |
| 51 |
/// </summary> |
| 52 |
/// <param name="inputPath_"></param> |
| 53 |
/// <param name="inputArguments_"></param> |
| 54 |
public void SetProcess(string inputPath_, string inputArguments_) |
| 55 |
{
|
| 56 |
procInfo.FileName = inputPath_; // 프로그램 경로 |
| 57 |
procInfo.Arguments = inputArguments_; // 매개변수의 경로 |
| 58 |
|
| 59 |
procInfo.WindowStyle = ProcessWindowStyle.Hidden; // cmd창이 숨겨지도록 하기 |
| 60 |
procInfo.CreateNoWindow = true; // cmd창을 띄우지 안도록 하기 |
| 61 |
|
| 62 |
procInfo.UseShellExecute = false; |
| 63 |
procInfo.RedirectStandardOutput = true; // cmd창에서 데이터를 가져오기 |
| 64 |
procInfo.RedirectStandardInput = true; // cmd창으로 데이터 보내기 |
| 65 |
procInfo.RedirectStandardError = true; // cmd창에서 오류 내용 가져오기 |
| 66 |
|
| 67 |
} |
| 68 |
|
| 69 |
/// <summary> |
| 70 |
/// 프로세스가 시작됩니다. |
| 71 |
/// </summary> |
| 72 |
public void StartProcess() |
| 73 |
{
|
| 74 |
try |
| 75 |
{
|
| 76 |
if(process == null) |
| 77 |
{
|
| 78 |
CreateProcess(); |
| 79 |
} |
| 80 |
|
| 81 |
process.Start(); // Process가 시작된다. |
| 82 |
|
| 83 |
process.BeginOutputReadLine(); // output 되는 라인들을 잡아오기 시작한다. |
| 84 |
|
| 85 |
isProcessAlive = true; |
| 86 |
} |
| 87 |
catch (Exception e) |
| 88 |
{
|
| 89 |
Console.WriteLine(e.ToString()); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
/// <summary> |
| 94 |
/// 프로세스에 메시지를 보냅니다. |
| 95 |
/// </summary> |
| 96 |
/// <param name="inputString_"></param> |
| 97 |
public void Msg(string inputString_) |
| 98 |
{
|
| 99 |
if (process.HasExited == true) |
| 100 |
process.Start(); |
| 101 |
|
| 102 |
process.StandardInput.Write(@inputString_ + Environment.NewLine); |
| 103 |
|
| 104 |
// 명령어를 보낼때는 꼭 마무리를 해줘야 한다. 그래서 마지막에 NewLine가 필요하다 |
| 105 |
//process.StandardInput.Close(); |
| 106 |
|
| 107 |
//string result = process.StandardOutput.ReadToEnd(); |
| 108 |
//StringBuilder sb = new StringBuilder(); |
| 109 |
//sb.Append("[Result Info]" + DateTime.Now + "\r\n");
|
| 110 |
//sb.Append(result); |
| 111 |
//sb.Append("\r\n");
|
| 112 |
|
| 113 |
//textBox1.Text = sb.ToString(); |
| 114 |
|
| 115 |
//Form_Manager.instance.Label_Result_Modify(sb.ToString()); |
| 116 |
|
| 117 |
//process.WaitForExit(); |
| 118 |
//process.Close(); |
| 119 |
} |
| 120 |
|
| 121 |
/// <summary> |
| 122 |
/// 프로세스가 종료됩니다. |
| 123 |
/// </summary> |
| 124 |
public void ExitProcess() |
| 125 |
{
|
| 126 |
process.CancelOutputRead(); |
| 127 |
|
| 128 |
process.Kill(); |
| 129 |
|
| 130 |
|
| 131 |
//process.StandardInput.Close(); |
| 132 |
//process.WaitForExit(); |
| 133 |
//process.Close(); |
| 134 |
} |
| 135 |
|
| 136 |
/// <summary> |
| 137 |
/// 프로세스가 꺼진 상황의 이벤트 입니다. |
| 138 |
/// </summary> |
| 139 |
/// <param name="sender"></param> |
| 140 |
/// <param name="e"></param> |
| 141 |
private void Process_Exited(object sender, EventArgs e) |
| 142 |
{
|
| 143 |
Console.WriteLine("process_exited");
|
| 144 |
|
| 145 |
isProcessAlive = false; |
| 146 |
} |
| 147 |
|
| 148 |
/// <summary> |
| 149 |
/// process 에서 출력되는 string 들을 list box에 출력해주는 함수. |
| 150 |
/// </summary> |
| 151 |
/// <param name="sender"></param> |
| 152 |
/// <param name="e"></param> |
| 153 |
private void OutputDataReceive_Handler(object sender, DataReceivedEventArgs e) |
| 154 |
{
|
| 155 |
if (resultListBox == null) |
| 156 |
return; |
| 157 |
|
| 158 |
resultListBox.Invoke(new Action(() => |
| 159 |
{
|
| 160 |
if (e.Data == null) |
| 161 |
return; |
| 162 |
|
| 163 |
resultListBox.Items.Add(e.Data); |
| 164 |
})); |
| 165 |
} |
| 166 |
} |
| 167 |
} |