2013-10-09 63 views
1

我的C#程序需要通过其标准输入将数据发送到第三方程序。但是,程序在处理之前会等待输入流到达EOF。在这里我的代码:C# - 子进程关闭输入流

// Starts the process. 
var process = new Process(); 
process.StartInfo.CreateNoWindow = true; 
process.StartInfo.UseShellExecute = false; 
process.StartInfo.RedirectStandardInput = true; 
process.StartInfo.RedirectStandardOutput = true; 
process.StartInfo.FileName = "foo.exe"; 
process.Start(); 

// Sends data to child process. 
var input = process.StandardInput; 
input.WriteLine("eval('2 * PI')"); 
input.Flush(); 
input.Close(); 

// Reads the result. 
var output = process.StandardOutput; 
var result = output.ReadLine(); 

儿童节目不会做任何事情,我的C#代码变为output.ReadLine()电话卡。但是,如果我杀死了C#进程,那么孩子开始正确地处理我发送的数据。在我还活着的时候,如何让孩子遇到EOF?

回答

2

StreamWriter在关闭流时可能不会发送实际的eof。您可以在关闭之前尝试将自己的内容写入流中。像这样的东西可能会工作:

input.Write((char)26); 

您可能需要找出该过程期望的eof。

+0

谢谢,但没有奏效。该过程期望文件的实际结束,而不是内联EOF字符。 – fernacolo

+0

如果关闭流并不意味着文件结束它有什么期望?也许将输入重定向回程序会给程序提供它所需要的信号。 – tinstaafl

+1

这对我有效。我从来没有意识到Windows使用SUB(0x1A)作为EOF,而不是像unix一样使用EOT(0x04)。 –