in reply to Connecting Perl's STDIN and STDOUT to .Net StreamReader

This sounds to me as if you are Suffering From Buffering, possibly connected with a bad use of the .Peek() method. I presume that the .Peek() method will return a false value whenver no data is available from the filehandle, that is, whenever you are reading too fast from your child. I don't see any mention of the .Peek() method in the System.Diagnostics.Process.Standardoutput Documentation, but it also mentions the buffering issues you're likely to encounter.

Replies are listed 'Best First'.
Re^2: Connecting Perl's STDIN and STDOUT to .Net StreamReader
by bsdz (Friar) on Sep 03, 2008 at 11:45 UTC
    The .Net Peek() method actually belongs to the StreamReader class of Which Sys.Diag.Process.Stdout is an instance of. It always returns the next char in a Stream or -1 if the Stream is closed or doesn't support Peek. I would hope otherwise it would block if the Stream is open and just empty. I will re-examine this part of the documentation.

    I am suspicious of Perl's buffering. However, when I try running a similar Perl process using Win32 command with type(cat) it works fine.

      I think Corion called it right. From the docs (my emphasis):

      The current position of the StreamReader object is not changed by Peek. The returned value is -1 if no more characters are currently available

      That suggests .Peek will return -1 when there is nothing currently available. Not when nothing more will ever become available (in a microsecond from now).

      Your fix is probabaly to use the "standard c# idiom":

      using (StreamReader sr = ... ) { string line; // Read and display lines from the file until the end +of // the file is reached. while ((line = sr.ReadLine()) != null) { ... } } }

      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
Re^2: Connecting Perl's STDIN and STDOUT to .Net StreamReader
by bsdz (Friar) on Sep 03, 2008 at 13:23 UTC
    After looking at it more closely and reading BrowserUK's message it appears that Peek does behave as you suggested. I eventually fixed it by using a blocking read instead. I.e.
    int totalBytes = 0; int count; char[] buffer = new char[4096]; while ( (count = process.StandardOutput.ReadBlock(buffer, 0, buf +fer.Length)) > 0 ) { totalBytes += count; }
    Sorry for it not being ultimately a Perl question but thanks in any case :)