public int Start() { var process = new Process(); process.StartInfo.FileName = Command; process.StartInfo.Arguments = Arguments; process.StartInfo.CreateNoWindow = true; process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardInput = true; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; process.StartInfo.StandardOutputEncoding = new UTF8Encoding(false); process.StartInfo.StandardErrorEncoding = new UTF8Encoding(false); runtimeValues = new RuntimeValueSet { Process = process, OutputBuffer = new StringBuilder(), OutputBufferLock = new object(), ErrorBuffer = new StringBuilder(), ErrorBufferLock = new object(), TimeoutMilliseconds = TimeoutMilliseconds, NewLine = NewLine, PromptWithNewLine = PromptWithNewLine, }; process.OutputDataReceived += (sender, e) => { if (e.Data == null) return; lock (runtimeValues.OutputBufferLock) { runtimeValues.OutputBuffer.AppendLine(e.Data); } }; process.ErrorDataReceived += (sender, e) => { if (e.Data == null) return; lock (runtimeValues.ErrorBufferLock) { runtimeValues.ErrorBuffer.AppendLine(e.Data); } }; process.Start(); process.BeginOutputReadLine(); process.BeginErrorReadLine(); if (ScriptToSetPrompt != null) { foreach (var line in ScriptToSetPrompt) { WriteLine(line); } } return process.Id; }
public void Stop(int processID) { if (runtimeValues == null) throw new InvalidOperationException(); if (!runtimeValues.Process.HasExited) { runtimeValues.Process.Kill(); } runtimeValues.Process.Close(); runtimeValues = null; }