加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 执行流程链

发布时间:2020-12-15 21:16:12 所属栏目:百科 来源:网络整理
导读:public void ExecuteProcessChain(string[] asProcesses,string sInRedirect,string sOutRedirect) { Process p1 = new Process(); p1.StartInfo.UseShellExecute = false; p1.StartInfo.RedirectStandardOutput = true; p1.StartInfo.FileName = asProcesse
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”,但是当我这样做时,它会卡在线上而((s = sr.ReadLine())!= null)并且只有在我关闭计算器之后代码才会继续.我需要两个计算器一起工作.你知道怎么做吗?

解决方法

ReadLine正在读取第一个计算的输出. Calc不发送任何输出.因此,ReadLine将永远不会返回,因此下一个计算将无法启动.当第一个计算终止时,ReadLine无法再从第一个计算中读取,因此返回null.返回后,代码可以启动第二个计算.

您可以不读取第一个calc或异步读取.
您可能想参考Async ReadLine如何
?异步读取.

您也可以在开始调用ReadLine之前使用p2启动第二个计算.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读