On Apr 5, 12:20 pm, "Jon Davis" <j...@REMOVE.ME.PLEASE.jondavis.net>
wrote:
> Does anyone know why using System.Diagnostics.Process to "wrap" a console
> application does not always transmit the I/O, depending on what processes
> you're trying to "consume"? PowerShell, for example, does not seem to
> process any I/O through the Process object.
>
> I know that in the case of PowerShell there are better ways to "wrap" the
> console by directly interfacing with the assemblies of
> System.Management.Automation or some similarly named namespace, but I'm
> trying to use generic command line wrappers for multiple types of processes
> that use the console I/O, and PowerShell was a handy example of why this
> won't work.
>
> ProcessStartInfo psi = new ProcessStartInfo(
> @"C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe");
> psi.Arguments = "-NoLogo";
> psi.UseShellExecute = false;
> psi.CreateNoWindow = true;
> psi.RedirectStandardInput = true;
> psi.RedirectStandardOutput = true;
> //psi.RedirectStandardError = true;
> Process process = new Process();
> process.StartInfo = psi;
> bool started = process.Start();
> if (started)
> {
> process.StandardInput.WriteLine("2+2");
> process.StandardInput.Flush();
> string ret = process.StandardOutput.ReadLine(); // <-- stalls here
> System.Console.WriteLine("PowerShell says \"2+2=" + ret + "\".");
>
> }
>
> Another one I was trying to "wrap" was the original implementation of Dave
> Raggett's HTML Tidy. The stuff below sometimes stalls on ReadToEnd(). It
> seemed to always stall until I added "process.StandardInput.Close();" after
> "process.StandardInput.Flush();" but it still stalls on ReadToEnd() half the
> time.
>
> ..
> _TidyProcessStartInfo = new ProcessStartInfo(
> Directory.GetParent(System.Reflection.Assembly.GetExecutingAssembly().Location)
> + "\\tidy.exe");
> _TidyProcessStartInfo.UseShellExecute = false;
> _TidyProcessStartInfo.CreateNoWindow = true;
> _TidyProcessStartInfo.RedirectStandardInput = true;
> _TidyProcessStartInfo.RedirectStandardOutput = true;
> _TidyProcessStartInfo.RedirectStandardError = true;
> . . .
> Process process = new Process();
> process.StartInfo = _TidyProcessStartInfo;
> process.ErrorDataReceived += new
> DataReceivedEventHandler(Exe_ErrorDataReceived);
> bool started = process.Start();
> if (started)
> {
> //process.StandardInput.AutoFlush = true;
> process.StandardInput.WriteLine(input);
> process.StandardInput.Flush();
> process.StandardInput.Close();
> ret = process.StandardOutput.ReadToEnd();
>
> }
>
> Thanks,
> Jon
You are probably hitting a deadlock issue when redirecting out. See
the docs on the RedirectStandardOutput property for more information:
http://msdn2.microsoft.com/en-us/lib...ardoutput.aspx
Chris