执行流程链

  public void ExecuteProcessChain(string[] asProcesses, string sInRedirect, string sOutRedirect)
    {
            Process p1 = new Process();
            p1.StartInfo.UseShellExecute = false;
            p1.StartInfo.RedirectStandardOutput = true;
            p1.StartInfo.FileName = asProcesses[0];
            p1.Start();
            StreamReader sr = p1.StandardOutput;
            string s, xxx = "";
            while ((s = sr.ReadLine()) != null)
                Console.WriteLine("sdfdsfs");
                //xxx += s+"n";
            p1.StartInfo.RedirectStandardInput = true;
            p1.StartInfo.RedirectStandardOutput = false;
            p1.StartInfo.FileName = asProcesses[1];
            p1.Start();
            StreamWriter sw = p1.StandardInput;
            sw.Write(xxx);
            sw.Close();
            sr.Close();

    }
我正在尝试执行“calc | calc”,但是当我这样做时,它会卡在
while ((s = sr.ReadLine()) != null)
行,并且只有在我关闭计算器之后代码才会继续。我需要两个计算器一起工作。你知道怎么做吗?     
已邀请:
ReadLine
正在读取第一个计算的输出。 Calc不发送任何输出。因此,
ReadLine
永远不会返回,因此下一个计算将不会开始。当第一个计算终止时,
ReadLine
不能再从第一个计算中读取,因此返回null。返回后,代码可以启动第二个计算。 您可以不读取第一个calc或异步读取。 您可能想要参考Async ReadLine的方法  异步读取。 你可以在开始调用
ReadLine
之前用p2开始第二次计算。     
为什么不使用线程? 考虑一下:将每个calc放入一个线程然后启动它们。之后让程序等待它们。只有在两个线程完成其作业(读取数据)后,您才能继续。 请记住,线程无法直接更改来自另一个线程的数据,因此我可能会建议使用Invoke或静态变量,具体取决于您可能需要的内容。 如果可能,您可以使用任务/并行库,它已经有一些有用的方法来帮助您。 背景工人也是一种方式。     

要回复问题请先登录注册