public static void RunGraph(string graph_path, ReadInput input_function) { if (CurrentProcess != null && !CurrentProcess.HasExited) { CurrentProcess.Kill(); } CurrentProcess = FetchNewProcess(graph_path); Aborting = false; if (input_function != null) { CurrentProcess.StartInfo.RedirectStandardInput = true; } try { //Start process and asynchronous reading CurrentProcess.Start(); CurrentProcess.BeginOutputReadLine(); CurrentProcess.BeginErrorReadLine(); if (input_function != null) { using (StreamWriter writer = new StreamWriter(CurrentProcess.StandardInput.BaseStream, Encoding.ASCII)) { while (!CurrentProcess.HasExited && !Aborting) { byte[] input = input_function(); if (input != null && input.Length != 0) { writer.WriteLine(Encoding.ASCII.GetString(input)); } else { writer.Flush(); } } } } if (Aborting && !CurrentProcess.HasExited) { CurrentProcess.Kill(); CurrentProcess.WaitForExit(); VSLogger.Log("Aborted current process."); } } catch (Win32Exception exception) { VSLogger.LogError("Output:\n" + exception.ToString()); } CurrentProcess.WaitForExit(); CurrentProcess = null; Aborting = false; }
private async Task <CommandResult> ExecuteAsyncInternal(string executable, string args) { var stdOut = new List <String>(); var stdErr = new List <String>(); CurrentProcess = CreateProcess(executable, args); CurrentProcess.ErrorDataReceived += (s, e) => { stdErr.Add(e.Data); var handler = ErrorDataReceived; if (handler != null) { handler(s, e); } }; CurrentProcess.OutputDataReceived += (s, e) => { stdOut.Add(e.Data); var handler = OutputDataReceived; if (handler != null) { handler(s, e); } }; var completionTask = CurrentProcess.StartAndWaitForExitAsync(); CurrentProcess.BeginOutputReadLine(); CurrentProcess.BeginErrorReadLine(); await completionTask; if (!CurrentProcess.WaitForExit(TimeoutMiliseconds)) { throw new TimeoutException($"The process failed to exit after {TimeoutMiliseconds / 1000.0} seconds."); } RemoveNullTerminator(stdOut); RemoveNullTerminator(stdErr); return(new CommandResult( CurrentProcess.StartInfo, CurrentProcess.ExitCode, String.Join(System.Environment.NewLine, stdOut), String.Join(System.Environment.NewLine, stdErr))); }
private async Task <CommandResult> ExecuteAsyncInternal(string executable, string args) { var output = new List <string>(); CurrentProcess = CreateProcess(executable, args); CurrentProcess.ErrorDataReceived += (s, e) => { if (e.Data == null) { return; } output.Add($"[{_label}] {e.Data}"); Console.WriteLine($"[{_label}] {e.Data}"); ErrorDataReceived?.Invoke(s, e); }; CurrentProcess.OutputDataReceived += (s, e) => { if (e.Data == null) { return; } output.Add($"[{_label}] {e.Data}"); Console.WriteLine($"[{_label}] {e.Data}"); OutputDataReceived?.Invoke(s, e); }; var completionTask = CurrentProcess.StartAndWaitForExitAsync(); CurrentProcess.BeginOutputReadLine(); CurrentProcess.BeginErrorReadLine(); await completionTask; CurrentProcess.WaitForExit(); RemoveNullTerminator(output); return(new CommandResult( CurrentProcess.StartInfo, CurrentProcess.ExitCode, string.Join(System.Environment.NewLine, output))); }
private async Task <CommandResult> ExecuteAsyncInternal(string executable, string args) { var stdOut = new List <String>(); var stdErr = new List <String>(); CurrentProcess = CreateProcess(executable, args); CurrentProcess.ErrorDataReceived += (s, e) => { stdErr.Add(e.Data); var handler = ErrorDataReceived; if (handler != null) { handler(s, e); } }; CurrentProcess.OutputDataReceived += (s, e) => { stdOut.Add(e.Data); var handler = OutputDataReceived; if (handler != null) { handler(s, e); } }; var completionTask = StartAndWaitForExitAsync(CurrentProcess); CurrentProcess.BeginOutputReadLine(); CurrentProcess.BeginErrorReadLine(); await completionTask; CurrentProcess.WaitForExit(); RemoveNullTerminator(stdOut); RemoveNullTerminator(stdErr); var stdOutString = String.Join(System.Environment.NewLine, stdOut); var stdErrString = String.Join(System.Environment.NewLine, stdErr); if (!string.IsNullOrWhiteSpace(stdOutString)) { Logger.LogDebug("stdout: {out}", stdOutString); } if (!string.IsNullOrWhiteSpace(stdErrString)) { Logger.LogDebug("stderr: {err}", stdErrString); } return(new CommandResult( CurrentProcess.StartInfo, CurrentProcess.ExitCode, stdOutString, stdErrString)); }