可重复喂养输入到一个进程的标准输入 [英] Repeatably Feeding Input to a Process' Standard Input
问题描述
我有一个(C#)控制台一种保持状态的应用程序。状态,其可通过控制台馈送与各种输入该应用程序来改变。我需要能够既饲料用比特输入的应用程序,然后读取输出漂洗并重复
I have a (C#) console application which maintains a state. The state can be altered by feeding the application with various input through the console. I need to be able to both feed the application with a bit of input, then read the output rinse and repeat.
我创建了一个新的进程,并尽一切重定向输入/输出的正常工作。现在的问题是,我已经发出后输入和呼叫的ReadLine()
在标准输出不前,我打电话返回一个值关闭()
后,我不能写了输入流的标准输入。
I create a new process and do all of the normal work of redirecting the input/output. The problem is that after I've sent input and call ReadLine()
on the standard output it does not return a value before I call Close()
on the standard input after which I cannot write anymore to the input stream.
我怎样才能保持打开输入流,同时还接收输出?
How can I keep open the input stream while still receiving output?
var process = new Process
{
StartInfo =
{
FileName =
@"blabal.exe",
RedirectStandardInput = true,
RedirectStandardError = true,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true,
ErrorDialog = false
}
};
process.EnableRaisingEvents = false;
process.Start();
var standardInput = process.StandardInput;
standardInput.AutoFlush = true;
var standardOutput = process.StandardOutput;
var standardError = process.StandardError;
standardInput.Write("ready");
standardInput.Close(); // <-- output doesn't arrive before after this line
var outputData = standardOutput.ReadLine();
process.Close();
process.Dispose();
控制台应用程序,我从重定向IO是很简单的。它从控制台读取使用 Console.Read()
和使用 Console.Write()
写它。我肯定知道这个数据是可读的,因为我从它使用标准输入/输出(写在.NET)。
The console application I'm redirecting IO from is very simple. It reads from the console using Console.Read()
and writes to it using Console.Write()
. I know for certain that this data is readable, since I have another application that reads from it using standard output / input (not written in .NET).
推荐答案
这是正在发生的事情,因为你正在使用写(准备好了)
这是会追加一个字符串到文本,而不是使用的WriteLine(准备好了)
。就这么简单:)
That is happening because of you are using Write("ready")
which is will append a string to the text, instead use WriteLine("ready")
. that simple :).
这篇关于可重复喂养输入到一个进程的标准输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!