4

I have a spawned process that spews about 3MB/second to stdout. I want to capture all of this data. The .NET Process.Start has the RedirectStandardOutput property that creates a FileStream object attached to the stdout file handle. It creates that FileStream with a 4096 byte buffer size. It's too puny for my needs. By the time I can start the background thread to read the redirected stdout, I've already missed several hundred kilobytes of data. Is there any way around this? Here is my code:

        var psi = new ProcessStartInfo
        {
            Arguments = CreateArgs(@"..."),
            CreateNoWindow = true,
            FileName = @"...",
            RedirectStandardOutput = true,
            RedirectStandardError = true,
            UseShellExecute = false,
            WorkingDirectory = @"...",
        };

        var process = new Process{StartInfo = psi, EnableRaisingEvents = true };
        process.ErrorDataReceived += (sender, args) => Dispatcher.Invoke(() => _stderr.AppendText(args.Data)); // log this
        process.Start();
        Task.Run(() =>
        {
            var buffer = new byte[size];
            using (var reader = new BinaryReader( ((FileStream)process.StandardOutput.BaseStream).))
            {
                while (!process.HasExited)
                {
                    reader.Read(buffer, 0, buffer.Length);
...
Brannon
  • 5,324
  • 4
  • 35
  • 83
  • Why don't you hook up the binary reader before you start the process? (you may need to use a 2nd thread) – Scott Chamberlain Feb 15 '14 at 05:37
  • I attempted that approach. BaseStream is null before the start function. – Brannon Feb 15 '14 at 05:44
  • Hmmm, I've just tried to read output via Process.StandardOutput and it works well with child that writes ~44 MB/s to stdout. Nothing was missed out. Show more code, maybe the problem hides in the other place. – nitrocaster Feb 19 '14 at 20:23
  • Have you attempted something like this: http://stackoverflow.com/questions/4143281/capturing-binary-output-from-process-standardoutput – Simon Feb 26 '14 at 13:02
  • Thanks for the link. I think that link is a valid duplicate of this question. – Brannon Feb 26 '14 at 15:38

0 Answers0