public static async Task CopyToEndAsync(this IPipeReader input, Stream stream, int bufferSize, CancellationToken cancellationToken) { try { await input.CopyToAsync(stream, bufferSize, cancellationToken); } catch (Exception ex) { input.Complete(ex); return; } return; }
public static Task CopyToAsync(this IPipeReader input, Stream stream) { return(input.CopyToAsync(stream, 4096, CancellationToken.None)); }
public static IPipeReader CreateFromCommand(this PipeFactory pfac, string filePath, string args , IPipeWriter stderr = null, IPipeReader stdin = null, CancellationToken ct = default(CancellationToken)) { var si = new ProcessStartInfo(filePath, args); si.RedirectStandardOutput = true; si.RedirectStandardError = stderr != null; si.RedirectStandardInput = stdin != null; si.UseShellExecute = false; si.CreateNoWindow = true; return(pfac.CreateReader(null, async(r, w) => { try { using (var proc = new Process()) { proc.StartInfo = si; proc.Start(); var procReader = proc.StandardOutput.BaseStream.AsPipelineReader(); var errReader = stderr != null ? proc.StandardError.BaseStream.AsPipelineReader() : null; await Task.WhenAll( Task.Run(async() => { if (stdin != null) { await stdin.CopyToAsync(proc.StandardInput.BaseStream).ConfigureAwait(false); proc.StandardInput.Dispose(); } }) , Task.Run(async() => { if (stderr != null) { await errReader.CopyToAsync(stderr).ConfigureAwait(false); } }) , Task.Run(() => { try { using (ct.Register(() => proc.Kill())) { proc.WaitForExit(); } } catch (Exception e) { procReader.Complete(e); procReader.CancelPendingRead(); if (errReader != null) { errReader.CancelPendingRead(); } } }) , Task.Run(async() => { await procReader.CopyToAsync(w).ConfigureAwait(false); }) ).ConfigureAwait(false); w.Complete(); } } catch (Exception e) { Console.WriteLine($"{e}"); w.Complete(e); } })); }