/// <summary> /// Waits for the process termination. If the process does not finish on time, it is terminated. /// </summary> /// <param name="TimeoutMs">Number of milliseconds to wait for the process exit before it is considered as failed and the process is terminated.</param> /// <returns>true if the function succeeds, false otherwise.</returns> public bool WaitExit(int TimeoutMs = 15000) { log.Trace("(TimeoutMs:{0})", TimeoutMs); bool res = false; if (Status != ConsoleProcessStatus.Running) { return(res); } try { res = process.WaitForExit(TimeoutMs); processExitCode = process.ExitCode; } catch (Exception e) { log.Error("Exception occurred: {0}", e.ToString()); } if (!res) { KillProcess(); } Status = ConsoleProcessStatus.Finished; log.Trace("(-):{0}", res); return(res); }
/// <summary> /// Creates a new instance of the object. /// </summary> /// <param name="Executable">Main process executable. May or may not include full path.</param> /// <param name="Arguments">Command line arguments to run the process with.</param> /// <param name="Input">Console input to be sent to the process once it is started.</param> public ConsoleProcess(string Executable, string Arguments = null, byte[] Input = null) { log.Trace("(Executable:'{0}',Arguments:'{1}')", Executable, Arguments); executable = Executable; arguments = Arguments; inputData = Input; Status = ConsoleProcessStatus.Initialized; log.Trace("(-)"); }
/// <summary> /// Starts the process. /// </summary> /// <returns>true if the function succeeds, false otherwise.</returns> public bool Run() { log.Trace("()"); bool res = false; outputData = new List <string>(); try { process = new Process(); process.StartInfo.FileName = executable; process.StartInfo.Arguments = arguments; process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardInput = true; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; process.StartInfo.StandardErrorEncoding = Encoding.UTF8; process.StartInfo.StandardOutputEncoding = Encoding.UTF8; process.OutputDataReceived += new DataReceivedEventHandler(ProcessOutputHandler); process.ErrorDataReceived += new DataReceivedEventHandler(ProcessOutputHandler); process.EnableRaisingEvents = true; log.Debug("Starting process '{0}'...", GetCommandLine()); if (process.Start()) { Status = ConsoleProcessStatus.Running; log.Debug("Process is running."); try { process.BeginOutputReadLine(); process.BeginErrorReadLine(); if ((inputData != null) && (inputData.Length > 0)) { using (StreamWriter sw = new StreamWriter(process.StandardInput.BaseStream, Encoding.UTF8)) { sw.Write(inputData); } } } catch (IOException e) { if (e.Message == "Broken pipe") { log.Trace("Process finished already."); Status = ConsoleProcessStatus.Finished; } else { throw e; } } res = true; } } catch (Exception e) { log.Trace("Exception occurred: {0}", e.ToString()); if (Status == ConsoleProcessStatus.Running) { KillProcess(); } Status = ConsoleProcessStatus.Finished; } log.Trace("(-):{0}", res); return(res); }