C#和Delphi之间的管道,如何使其工作?

时间:2012-10-12 04:50:44

标签: c# delphi process pipe

我有一个用Delphi编写的控制台应用程序(Host.exe)。它接受stdin readln和writeln对stdout的响应。 现在,我想以C#为Host.exe输入并从Host.exe获取输出的方式在C#应用程序中使用Host.exe 理想情况下,我编写下面的代码,但它不起作用:它挂在outputReader.ReadLine()中的某处;

System.IO.File.WriteAllText(tmp, vbs);

Process pProcess = new Process();

pProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal;

//strCommand is path and file name of command to run
pProcess.StartInfo.FileName = @"Host.exe";
pProcess.StartInfo.Arguments = "\"runa " + tmp +"\"";
// runs script file tmp in background
pProcess.StartInfo.CreateNoWindow = true;
pProcess.StartInfo.UseShellExecute = false;
pProcess.StartInfo.RedirectStandardOutput = true;
pProcess.StartInfo.RedirectStandardInput = true;
pProcess.StartInfo.RedirectStandardError = true;

pProcess.Start();

StreamWriter inputWriter = pProcess.StandardInput;
StreamReader outputReader = pProcess.StandardOutput;

while (true)
{
    inputWriter.WriteLine("getmsg");
    inputWriter.Flush();
    string s = outputReader.ReadLine(); // then do something with it

    inputWriter.WriteLine("progressglobal");
    inputWriter.Flush();

    string p = outputReader.ReadLine();
    if (p == "100")
    {
        break;
    }
    Application.DoEvents();
    Thread.Sleep(1000);
}
inputWriter.WriteLine("exit");
inputWriter.Flush();                    
pProcess.WaitForExit();

非常感谢您提前提出任何建议!

1 个答案:

答案 0 :(得分:0)

你读了两行:

string s = outputReader.ReadLine();

string p = outputReader.ReadLine();

似乎您只需要最后一行,因为未使用变量s