public ProcessResult RemoteExecute(ProcessInfo processInfo, OnProcessOutput output) { ProcessStartInfo startInfo = new ProcessStartInfo(); var filteredEnvironmentVariables = new Dictionary <string, string>(); foreach (DictionaryEntry entry in processInfo.EnvironmentVariables) { string name = (string)entry.Key; string value = (string)entry.Value; if (startInfo.EnvironmentVariables[name] == value) { continue; // skip locally defined variables } filteredEnvironmentVariables.Add(name, value); } StringBuilder stdoutBuilder = new StringBuilder(); StringBuilder stderrBuilder = new StringBuilder(); int exitCode = Execute(processInfo.FileName, processInfo.Arguments, string.IsNullOrEmpty(processInfo.WorkingDirectory) ? null : processInfo.WorkingDirectory, filteredEnvironmentVariables, line => { stdoutBuilder.AppendLine(line); output(new ProcessOutputEventArgs(ProcessOutputType.StandardOutput, line)); }, line => { stderrBuilder.AppendLine(line); output(new ProcessOutputEventArgs(ProcessOutputType.ErrorOutput, line)); }, TimeSpan.FromMilliseconds(processInfo.TimeOut)); return(new ProcessResult( stdoutBuilder.ToString(), stderrBuilder.ToString(), exitCode, exitCode == -1, !processInfo.ProcessSuccessful(exitCode))); }
public ProcessResult RemoteExecute(ProcessInfo processInfo, OnProcessOutput output) { ProcessStartInfo startInfo = new ProcessStartInfo(); var filteredEnvironmentVariables = new Dictionary<string, string>(); foreach (DictionaryEntry entry in processInfo.EnvironmentVariables) { string name = (string)entry.Key; string value = (string)entry.Value; if (startInfo.EnvironmentVariables[name] == value) continue; // skip locally defined variables filteredEnvironmentVariables.Add(name, value); } StringBuilder stdoutBuilder = new StringBuilder(); StringBuilder stderrBuilder = new StringBuilder(); int exitCode = Execute(processInfo.FileName, processInfo.Arguments, string.IsNullOrEmpty(processInfo.WorkingDirectory) ? null : processInfo.WorkingDirectory, filteredEnvironmentVariables, line => { stdoutBuilder.AppendLine(line); output(new ProcessOutputEventArgs(ProcessOutputType.StandardOutput, line)); }, line => { stderrBuilder.AppendLine(line); output(new ProcessOutputEventArgs(ProcessOutputType.ErrorOutput, line)); }, TimeSpan.FromMilliseconds(processInfo.TimeOut)); return new ProcessResult( stdoutBuilder.ToString(), stderrBuilder.ToString(), exitCode, exitCode == -1, ! processInfo.ProcessSuccessful(exitCode)); }
public static async Task <ProcessStats> Run(string commandline, OnProcessOutput onProcessOutput, OnProcessCreated onProcessCreated) { var pi = new PROCESS_INFORMATION(); try { CreatePipeAsyncReadSyncWrite(out NamedPipeServerStream readerOut, out NamedPipeClientStream writerOut); CreatePipeAsyncReadSyncWrite(out NamedPipeServerStream readerErr, out NamedPipeClientStream writerErr); using (readerOut) using (writerOut) using (readerErr) using (writerErr) { pi = CreateProcess(commandline, writerOut, writerErr); onProcessCreated?.Invoke(pi.dwProcessId); // // 2019-09-22 Spindi himself // this Close() is very importante. // When we do not close the writer handle here, the read from out/err will hang. // writerOut.Close(); writerErr.Close(); Task stdout = Misc.ReadLinesAsync(new StreamReader(readerOut), (line) => onProcessOutput(KINDOFOUTPUT.STDOUT, line)); Task stderr = Misc.ReadLinesAsync(new StreamReader(readerErr), (line) => onProcessOutput(KINDOFOUTPUT.STDERR, line)); await Task.WhenAll(stdout, stderr); uint exitCode = GetExitCode(pi); return(CreateProcessStats(pi, exitCode)); } } finally { if (!pi.hProcess.Equals(IntPtr.Zero)) { CloseHandle(pi.hProcess); } } }
/// <summary> /// Fires the console output event. /// </summary> /// <param name="args">The <see cref="ProcessEventArgs"/> instance containing the event data.</param> private void FireProcessOutputEvent(ProcessEventArgs args) { // Fire the event if it is set. OnProcessOutput?.Invoke(this, args); }
/// <summary> /// Raises OnProcessOutput event /// </summary> /// <param name="outputChunk">The output from process</param> private void RaiseProcessOutputEvent(OutputChunk outputChunk) { OnProcessOutput?.Invoke(this, new ProcessEventArgs(outputChunk.Output, outputChunk.IsError)); }
/// <summary> /// Fires the process output event. /// </summary> /// <param name="content">The content.</param> protected virtual void FireProcessOutputEvent(string content) { // Get the event and fire it. OnProcessOutput?.Invoke(this, new ConsoleStreamEventArgs(content)); }
/// <summary> /// Fires the process output event. /// </summary> /// <param name="content">The content.</param> private void FireProcessOutputEvent(string content) => OnProcessOutput?.Invoke(this, new ProcessEventArgs(content));
/// <summary> /// Fires the process output event. /// </summary> /// <param name="content">The content.</param> private void FireProcessOutputEvent(string content) { // Get the event and fire it. OnProcessOutput?.Invoke(this, new ProcessEventArgs(content)); }