解决方案 »

  1.   


    StreamReader SR=P.StanderdOutput;
    while(!SR.EndOfStream)
    {
    string s=SR.ReadLine();
    }
      

  2.   

    猜测在写的一方可能要写一句flush一句,否则写的东西在buffer里,可能不是马上能读到
      

  3.   

    调用的方式不对吧
    完整代码:richTextBox1.Text = "";
                string[] commandlist = textBox1.Text.Split(' ');
                string command = string.Empty;
                for (int i = 1; i < commandlist.Length; i++)
                {
                    command += commandlist[i] + " ";
                }
                ProcessStartInfo start = new ProcessStartInfo(commandlist[0] + ".exe");//设置运行的命令行文件问ping.exe文件,这个文件系统会自己找到
                //如果是其它exe文件,则有可能需要指定详细路径,如运行winRar.exe
                start.Arguments = command;//设置命令参数
                start.CreateNoWindow = true;//不显示dos命令行窗口
                start.RedirectStandardOutput = true;//
                start.RedirectStandardInput = true;//
                start.UseShellExecute = false;//是否指定操作系统外壳进程启动程序
                Process p = null;
                StreamReader reader = null;
                try
                {
                    p = Process.Start(start);
                    reader = p.StandardOutput;//截取输出流
                    string line = reader.ReadLine();//每次读取一行
                    while (!reader.EndOfStream)
                    {
                        if (line.Length > 0)
                        {
                            richTextBox1.AppendText(line + "\n");
                        }
                        line = reader.ReadLine();
                        if (backgroundWorker1.CancellationPending)
                        {
                            break;
                        }
                    }
                    //p.WaitForExit();//等待程序执行完退出进程
                }
                catch(Exception ex)
                {
                    richTextBox1.Clear();
                    richTextBox1.Text = "无效指令";
                }
                finally
                {
                    try
                    {
                        p.Close();//关闭进程
                        reader.Close();//关闭流
                    }
                    catch { }
                }
      

  4.   

    我知道了,你在主线程里while,阻塞了UI显示了
    要么放到backgroudworker里(多线程)执行
    要么每次给text赋值,要执行Application.DoEvents()让UI刷新
      

  5.   

    我的process是在子线程里执行的
    private void tlbtn_run_Click(object sender, EventArgs e)
            {
                this.tlbtn_run.Enabled = false;
                this.rtxtOutput.Text = string.Empty;
                      
                // run python program
                ThreadStart run_py_dele = new ThreadStart(RunPy);
                Thread run_py_process = new Thread(run_py_dele);
                run_py_process.
            }
            public void RunPy()
            {
                SetButtonEnabel setEnable = new SetButtonEnabel(SetBtnEanbel);
                SetTreeViewNodeImageIndex setTreeNodeImage = new SetTreeViewNodeImageIndex(SetTreeNodeImageIndex);
                string filename = "python.exe";
                string[] _args = new string[2];
                Process p_run = new Process();
                p_run.StartInfo.FileName = filename;
                p_run.StartInfo.UseShellExecute = false;
                p_run.StartInfo.WorkingDirectory = Environment.CurrentDirectory;
                p_run.StartInfo.RedirectStandardInput = true;
                p_run.StartInfo.RedirectStandardOutput = true;
                p_run.StartInfo.RedirectStandardError = true;
                p_run.StartInfo.CreateNoWindow = true;
                //p_run.OutputDataReceived += new DataReceivedEventHandler(NetOutputDataHandler);
                //p_run.ErrorDataReceived += new DataReceivedEventHandler(NetErrorDataHandler);
                proc_output = new StringBuilder();
                TreeNode root = this.treev_case_list.Nodes[0];
                //模块级别
                foreach (TreeNode tnode in root.Nodes)
                {
                    //用例级别
                    foreach (TreeNode ttnode in tnode.Nodes)
                    {
                        error_flag = 0;
                        if (ttnode.Checked == true && ttnode.Tag.ToString() != "dir")
                        {
                            _args = get_Script_config(ttnode.Tag.ToString());
                            p_run.StartInfo.Arguments = string.Format("{0} -f {1}", _args[0], Environment.CurrentDirectory + "\\" + _args[1]);
                            p_run.Start();
                            StreamReader sr = p_run.StandardOutput;
                            while (!sr.EndOfStream)
                            {
                                LogOutput(sr.ReadLine());
                            }
                            StreamReader esr = p_run.StandardError;
                            while (!esr.EndOfStream)
                            {
                                error_flag = 1;
                                LogError(esr.ReadLine());
                            }
                            //if (flag == 0)
                            //{
                            //    p_run.BeginOutputReadLine();
                            //    p_run.BeginErrorReadLine();
                            //    flag = 1;
                            //}
                            
                            //p_run.WaitForExit();
                            //if (proc_output.Length > 0)
                            //{
                            //    LogOutput(proc_output.ToString());
                            //}                        //err_str = p_run.StandardError.ReadToEnd();
                            //if (!string.IsNullOrEmpty(err_str))
                            //{
                            //    LogError(err_str);
                            //}                        p_run.Close();
                            if (error_flag == 1)
                            {
                                this.Invoke(setTreeNodeImage, ttnode, 2);
                            }
                            else
                            {
                                this.Invoke(setTreeNodeImage, ttnode, 1);
                            }
                        }
                    }
                }
                this.Invoke(setEnable, new object[] { true});
                
            }      public void LogAppend(Color color, string text)
            {
                this.rtxtOutput.SelectionColor = color;
                this.rtxtOutput.AppendText(Encoding.UTF8.GetString(Encoding.Default.GetBytes(text)));
                this.rtxtOutput.AppendText("\n"); 
            }
    主线程负责启动子线程,然后所有的操作都在子线程里。上面是相关代码,请指正。多谢
      

  6.   

    LogOutput的代码在哪呢,是直接输出了,还是保存到哪里去了?
      

  7.   


    public void LogOutput(string text)
            {
                LogAppendDelegate la = new LogAppendDelegate(LogAppend);
                this.rtxtOutput.Invoke(la, Color.Black, text);
            }public void LogAppend(Color color, string text)
            {
                this.rtxtOutput.SelectionColor = color;
                this.rtxtOutput.AppendText(Encoding.UTF8.GetString(Encoding.Default.GetBytes(text)));
                this.rtxtOutput.AppendText("\n");
                Application.DoEvents();
            }到这里来了
    Application.DoEvents();加了这个也没用
      

  8.   

    这个代码放到cmd里执行,是一行一行的出来,还是一下出来500行?
      

  9.   

    至少在cmd里,是一行一行出来,你程序才能一行一行的读
    如果cmd里就是一下出来500行,你这边做啥都没用.
      

  10.   


    其实我想做的是这个,一个自动化测试工具,当我执行脚本的时候会有很多信息输出,之前都是在linux下运行的,但是很多人都喜欢GUI。所以想做一个GUI的运行工具,脚本每输出一行就在工具的下面打印一行。现在的情况是脚本执行完后,所有的信息才一次打印出来