Quantcast
Channel: Visual C# forum
Viewing all articles
Browse latest Browse all 31927

Process.StandardOutput.Read() & Peek() hang on empty buffer

$
0
0

Hello,

I need to asynchronously read the stdout stream of a processcharacter-at-a-time, even if there is no newline character.  My simplified but still failing code is below. I realize there is an infinite loop in it but that is not the issue. This code reads the characters fine until all of them have been read, but when it then goes back for another character it never returns. I've tried using Peek before I do the read and for some of my processes this fixes the problem.  However, for others Peek hangs too. I've tried using the debugger to step into Read and Peek but nothing happens.  My processes are nothing more than simple "Hello world" C and C++ console applications that also do a little inputting from stdin. Is it possible that if a process is paused and waiting for input from stdin Read and Peek will hang?  I'm using Win 8.1 Pro 64-bit, VS2013 Ultimate, and building for .NET 4.5.

Thanks,
Ray

  publicstaticvoidRunIt(){Process process =newProcess();
     process.StartInfo.FileName="D:\\temp\\C1A7E1.exe";
     process.StartInfo.UseShellExecute=false;
     process.StartInfo.RedirectStandardOutput=true;
     process.StartInfo.RedirectStandardError=true;
     process.StartInfo.RedirectStandardInput=true;
     process.StartInfo.CreateNoWindow=true;
     process.Start();string textString =string.Empty;for(;;){int outputCharInt;if((outputCharInt = process.StandardOutput.Read())!=-1)
           textString +=(char)outputCharInt;elseThread.Sleep(1000);}}


Viewing all articles
Browse latest Browse all 31927

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>