public static Stream ToStream(string filename, string arguments, Stream input) { Process externalAppProc = null; try { ProcessStartInfo psi = new ProcessStartInfo { StandardOutputEncoding = Encoding.GetEncoding(1250), FileName = filename, Arguments = arguments, CreateNoWindow = true, UseShellExecute = false, RedirectStandardInput = true, RedirectStandardOutput = true, RedirectStandardError = true }; externalAppProc = new Process(); externalAppProc.StartInfo = psi; externalAppProc.Start(); var outputStream = new ExtraDisposableStream(externalAppProc.StandardOutput.BaseStream, new IDisposable[] { externalAppProc }); ThreadPool.QueueUserWorkItem( parameter => { //todo: May take down all application in case of exception if (outputStream.Disposed) return; using (externalAppProc.StandardInput) { var buffer = new byte[64 * 1024]; int readBytes; while ((readBytes = input.Read(buffer, 0, buffer.Length)) > 0) { if (outputStream.Disposed) return; externalAppProc.StandardInput.BaseStream.Write(buffer, 0, readBytes); if (outputStream.Disposed) return; } externalAppProc.StandardInput.Flush(); externalAppProc.StandardInput.Close(); } }, externalAppProc ); return outputStream; } catch { if (externalAppProc != null) externalAppProc.Dispose(); throw; } }
public static Stream ToStream(string filename, string arguments, Stream input) { Process externalAppProc = null; try { ProcessStartInfo psi = new ProcessStartInfo { StandardOutputEncoding = Encoding.GetEncoding(1250), FileName = filename, Arguments = arguments, CreateNoWindow = true, UseShellExecute = false, RedirectStandardInput = true, RedirectStandardOutput = true, RedirectStandardError = true }; externalAppProc = new Process(); externalAppProc.StartInfo = psi; externalAppProc.Start(); var outputStream = new ExtraDisposableStream(externalAppProc.StandardOutput.BaseStream, new IDisposable[] { externalAppProc }); ThreadPool.QueueUserWorkItem( parameter => { //todo: May take down all application in case of exception if (outputStream.Disposed) { return; } using (externalAppProc.StandardInput) { var buffer = new byte[64 * 1024]; int readBytes; while ((readBytes = input.Read(buffer, 0, buffer.Length)) > 0) { if (outputStream.Disposed) { return; } externalAppProc.StandardInput.BaseStream.Write(buffer, 0, readBytes); if (outputStream.Disposed) { return; } } externalAppProc.StandardInput.Flush(); externalAppProc.StandardInput.Close(); } }, externalAppProc ); return(outputStream); } catch { if (externalAppProc != null) { externalAppProc.Dispose(); } throw; } }