当前位置:编程学习 > C#/ASP.NET >>

请问调用外部程序,重定向IO如何读取?

请问,调用外部程序,重定向IO如何读取?

如果用ReadToEnd,会阻塞当前进程,显示进程输出的字符串叶不能及时显示。
如果用ReadLine,也会阻塞当前进程,外部程序未必是一行一行的输出,显示进程输出的字符串叶不能及时显示。

如何能够一个字符一个字符的读取(貌似不是一个Byte一个Byte的读取),且不阻塞当前进程?


            Process p = new Process();
            p.StartInfo = new ProcessStartInfo();

            p.StartInfo.WorkingDirectory = util_misc.GetExePath() + "\\stk500";

            p.StartInfo.FileName = p.StartInfo.WorkingDirectory + '\\' + "stk500.exe";


            p.StartInfo.Arguments = " --help";
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.CreateNoWindow = true;

            p.Start();
            StreamReader reader;
            string input;
                
            reader = p.StandardOutput;
            input = reader.ReadLine();

            while (!reader.EndOfStream)
            {              
                this.Invoke(new AppendRichText(AppendText), input);
                input = reader.ReadLine();
            }

            input = p.StandardError.ReadToEnd();
            if (input != null)
            {
                this.Invoke(new AppendRichText(AppendText), input);
            }

            p.WaitForExit();


补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,