private void OutputDataReceived(object sender, DataReceivedEventArgs e) { if (e.Data != null) { _processOutput?.Invoke(e.Data); } }
private void ExecuteImpl_ReadOutput(Process process) { StringBuilder output = new StringBuilder(); StringBuilder error = new StringBuilder(); using (AutoResetEvent outputWaitHandle = new AutoResetEvent(false)) using (AutoResetEvent errorWaitHandle = new AutoResetEvent(false)) { process.OutputDataReceived += (sender, e) => { if (e.Data == null) { outputWaitHandle.Set(); } else { output.AppendLine(e.Data); } }; process.ErrorDataReceived += (sender, e) => { if (e.Data == null) { errorWaitHandle.Set(); } else { error.AppendLine(e.Data); } }; try { process.Start(); } catch (Win32Exception win32e) { OnProcessStartWin32Exception?.Invoke(win32e); return; } catch (Exception) { return; } process.BeginOutputReadLine(); process.BeginErrorReadLine(); if (process.WaitForExit(ExecutableTimeout) && outputWaitHandle.WaitOne() && errorWaitHandle.WaitOne()) { ProcessOutput?.Invoke(output, error); ProcessExit?.Invoke(); } else { try { process.Kill(); OnProcessTimeout?.Invoke(); } catch (Win32Exception) { } catch (NotSupportedException) { } catch (InvalidOperationException) { } } } }