対話型コマンドラインプログラムのラッパーを記述する必要があります。別の.NETプログラムからの対話型コマンドラインプログラムの使用方法
これは、標準入力を介して他のプログラムにコマンドを送信し、標準出力を介して応答を受け取ることができる必要があることを意味します。
入力ストリームが開いている間に標準出力ストリームがブロックされているように見えることが問題です。私が入力ストリームを閉じると、私は応答を得ます。しかし、私はより多くのコマンドを送ることはできません。
これは私が(ほとんどhereから)、現時点では使用していますものです:
void Main() {
Process process;
process = new Process();
process.StartInfo.FileName = "atprogram.exe";
process.StartInfo.Arguments = "interactive";
// Set UseShellExecute to false for redirection.
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
// Redirect the standard output of the command.
// This stream is read asynchronously using an event handler.
process.StartInfo.RedirectStandardOutput = true;
// Set our event handler to asynchronously read the output.
process.OutputDataReceived += (s, e) => Console.WriteLine(e.Data);
// Redirect standard input as well. This stream is used synchronously.
process.StartInfo.RedirectStandardInput = true;
process.Start();
// Start the asynchronous read of the output stream.
process.BeginOutputReadLine();
String inputText;
do
{
inputText = Console.ReadLine();
if (inputText == "q")
{
process.StandardInput.Close(); // After this line the output stream unblocks
Console.ReadLine();
return;
}
else if (!String.IsNullOrEmpty(inputText))
{
process.StandardInput.WriteLine(inputText);
}
}
}
私も同じ結果に、同期の標準出力ストリームを読んでみました。入力ストリームが閉じられるまで、出力ストリームのブロックを無期限に呼び出します。Peek()
およびEndOfStream
でも可能です。
全二重の方法で他のプロセスと通信する手段はありますか?
非同期スレッド? –
@BenderBendingはい私はできるが、これは助けになるだろうか? – Karsten
私はそう思う。一方のスレッドの入力ストリームと他方のスレッドの出力ストリーム –