/// <summary> /// Launches the specified executable as a child process, capturing its stdout and parsing its output, using a /// default processor, into a display segment set. /// </summary> /// /// <param name="executablePath">The path, including filename, to the executable to launch.</param> /// <param name="arguments">The arguments to pass to the child process.</param> /// <param name="workingPath">The working path to specify for the child process.</param> /// <param name="displaySegmentProcessor">The callback function responsible for processing any display segments parsed from the child process' stdout captures.</param> /// /// <remarks> /// This call will block until the child process exits. /// </remarks> /// public static void Launch(string executablePath, string arguments, string workingPath, DisplaySegmentProcessor displaySegmentProcessor) { ProcessManager.Launch(executablePath, arguments, workingPath, displaySegmentProcessor, ProcessManager.DefaultOutputParser); }
/// <summary> /// Launches the specified executable as a child process, capturing its stdout and parsing its output into /// a display segment set. /// </summary> /// /// <param name="executablePath">The path, including filename, to the executable to launch.</param> /// <param name="arguments">The arguments to pass to the child process.</param> /// <param name="workingPath">The working path to specify for the child process.</param> /// <param name="displaySegmentProcessor">The callback function responsible for processing any display segments parsed from the child process' stdout captures.</param> /// <param name="outputParser">The callback function responsible for parsing the output captured from the child process' stdout stream.</param> /// /// <remarks> /// This call will block until the child process exits. /// </remarks> /// public static void Launch(string executablePath, string arguments, string workingPath, DisplaySegmentProcessor displaySegmentProcessor, OutputParser outputParser) { if (executablePath == null) { throw new ArgumentNullException("executablePath"); } if (displaySegmentProcessor == null) { throw new ArgumentNullException("displaySegmentProcessor"); } if (outputParser == null) { throw new ArgumentNullException("outputParser"); } if (!File.Exists(executablePath)) { throw new ArgumentException("The path does not exist", "executablePath"); } var dataRecieveCompleted = new ManualResetEvent(false); var processStartInfo = new ProcessStartInfo { CreateNoWindow = true, RedirectStandardOutput = true, UseShellExecute = false, ErrorDialog = false, Arguments = arguments, FileName = executablePath, WorkingDirectory = workingPath }; DataReceivedEventHandler dataHandler = (sender, args) => { if ((args == null ) || (args.Data == null)) { dataRecieveCompleted.Set(); return; } var result = outputParser(args.Data); displaySegmentProcessor(result); }; using (var process = new Process()) { try { process.StartInfo = processStartInfo; process.OutputDataReceived += dataHandler; if (process.Start()) { process.BeginOutputReadLine(); process.WaitForExit(); // Because the data recieved can be sent after the process exited, wait // until a null data event happens or a second has passed to return. dataRecieveCompleted.WaitOne(TimeSpan.FromSeconds(1)); } } finally { if (process != null) { process.OutputDataReceived -= dataHandler; } } } }
/// <summary> /// Launches the specified executable as a child process, capturing its stdout and parsing its output into /// a display segment set. /// </summary> /// /// <param name="executablePath">The path, including filename, to the executable to launch.</param> /// <param name="arguments">The arguments to pass to the child process.</param> /// <param name="workingPath">The working path to specify for the child process.</param> /// <param name="displaySegmentProcessor">The callback function responsible for processing any display segments parsed from the child process' stdout captures.</param> /// <param name="outputParser">The callback function responsible for parsing the output captured from the child process' stdout stream.</param> /// /// <remarks> /// This call will block until the child process exits. /// </remarks> /// public static void Launch(string executablePath, string arguments, string workingPath, DisplaySegmentProcessor displaySegmentProcessor, OutputParser outputParser) { if (executablePath == null) { throw new ArgumentNullException("executablePath"); } if (displaySegmentProcessor == null) { throw new ArgumentNullException("displaySegmentProcessor"); } if (outputParser == null) { throw new ArgumentNullException("outputParser"); } if (!File.Exists(executablePath)) { throw new ArgumentException("The path does not exist", "executablePath"); } var dataRecieveCompleted = new ManualResetEvent(false); var processStartInfo = new ProcessStartInfo { CreateNoWindow = true, RedirectStandardOutput = true, UseShellExecute = false, ErrorDialog = false, Arguments = arguments, FileName = executablePath, WorkingDirectory = workingPath }; DataReceivedEventHandler dataHandler = (sender, args) => { if ((args == null) || (args.Data == null)) { dataRecieveCompleted.Set(); return; } var result = outputParser(args.Data); displaySegmentProcessor(result); }; using (var process = new Process()) { try { process.StartInfo = processStartInfo; process.OutputDataReceived += dataHandler; if (process.Start()) { process.BeginOutputReadLine(); process.WaitForExit(); // Because the data recieved can be sent after the process exited, wait // until a null data event happens or a second has passed to return. dataRecieveCompleted.WaitOne(TimeSpan.FromSeconds(1)); } } finally { if (process != null) { process.OutputDataReceived -= dataHandler; } } } }