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; }
public virtual void Start() { // Check to see if process is already busy if (Running) { throw new Exception("Process is already running."); } Dispose(); // Get the tool executable string toolPath = Tool; if (!Path.IsPathRooted(toolPath)) { toolPath = XilinxHelper.GetXilinxToolPath(Tool); if (toolPath == null) { throw new Exception(string.Format("Unable to location the executable for the tool '{0}'", Tool)); } } // Create the process with special Xilinx Environment CurrentProcess = CreateXilinxEnvironmentProcess(); CurrentProcess.StartInfo.FileName = toolPath; string args = ""; foreach (string arg in Arguments) { if (!string.IsNullOrEmpty(args)) { args += " "; } args += arg; } CurrentProcess.StartInfo.Arguments = args; CurrentProcess.StartInfo.WorkingDirectory = WorkingDirectory; CurrentProcess.StartInfo.UseShellExecute = false; if (RedirectOutput) { CurrentProcess.StartInfo.RedirectStandardError = true; CurrentProcess.StartInfo.RedirectStandardOutput = true; listener = new ProcessHelper.ProcessListener(CurrentProcess); listener.StdOutNewLineReady += delegate(string line) { if (line != null) { ProcessLine(line); } }; listener.StdErrNewLineReady += delegate(string line) { if (line != null) { ProcessErrorLine(line); } }; } if (RedirectInput) { CurrentProcess.StartInfo.RedirectStandardInput = true; } // Start the process CurrentProcess.Start(); if (RedirectOutput) { // Start the listener listener.Begin(); } }
private Task<CommandResult> RunProcessAsync(string executable, string args, StreamForwarder stdOut, StreamForwarder stdErr) { Task taskOut = null; Task taskErr = null; CurrentProcess = CreateProcess(executable, args); CurrentProcess.Start(); try { taskOut = stdOut.BeginRead(CurrentProcess.StandardOutput); } catch (System.InvalidOperationException e) { if (!e.Message.Equals("The collection has been marked as complete with regards to additions.")) { throw; } } try { taskErr = stdErr.BeginRead(CurrentProcess.StandardError); } catch (System.InvalidOperationException e) { if (!e.Message.Equals("The collection has been marked as complete with regards to additions.")) { throw; } } var tcs = new TaskCompletionSource<CommandResult>(); CurrentProcess.Exited += (sender, arg) => { var tasksToAwait = new List<Task>(); if (taskOut != null) { tasksToAwait.Add(taskOut); } if (taskErr != null) { tasksToAwait.Add(taskErr); } if (tasksToAwait.Any()) { try { Task.WaitAll(tasksToAwait.ToArray()); } catch (System.ObjectDisposedException e) { taskErr = null; taskOut = null; } } var result = new CommandResult( CurrentProcess.StartInfo, CurrentProcess.ExitCode, stdOut?.CapturedOutput ?? CurrentProcess.StandardOutput.ReadToEnd(), stdErr?.CapturedOutput ?? CurrentProcess.StandardError.ReadToEnd()); tcs.SetResult(result); }; return tcs.Task; }
private CommandResult RunProcess(string executable, string args, StreamForwarder stdOut, StreamForwarder stdErr) { Task taskOut = null; Task taskErr = null; CurrentProcess = CreateProcess(executable, args); CurrentProcess.Start(); try { taskOut = stdOut.BeginRead(CurrentProcess.StandardOutput); } catch (System.InvalidOperationException e) { if (!e.Message.Equals("The collection has been marked as complete with regards to additions.")) { throw; } } try { taskErr = stdErr.BeginRead(CurrentProcess.StandardError); } catch (System.InvalidOperationException e) { if (!e.Message.Equals("The collection has been marked as complete with regards to additions.")) { throw; } } CurrentProcess.WaitForExit(); var tasksToAwait = new List <Task>(); if (taskOut != null) { tasksToAwait.Add(taskOut); } if (taskErr != null) { tasksToAwait.Add(taskErr); } if (tasksToAwait.Any()) { Task.WaitAll(tasksToAwait.ToArray()); } var result = new CommandResult( CurrentProcess.StartInfo, CurrentProcess.ExitCode, stdOut?.CapturedOutput ?? CurrentProcess.StandardOutput.ReadToEnd(), stdErr?.CapturedOutput ?? CurrentProcess.StandardError.ReadToEnd()); return(result); }