我如何可以重定向过程输出(控制台)到RichTextBox的? [英] How can I redirect process output (console) to richtextbox?

查看:257
本文介绍了我如何可以重定向过程输出(控制台)到RichTextBox的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是错的,为什么是RichTextBox的犯规获得过程输出流?在孤单RichTextBox的..

 私人无效的button1_Click(对象发件人,EventArgs五)
{$ B $无文字显示b
流程sortProcess;
sortProcess =新工艺();
sortProcess.StartInfo.FileName =SORT.EXE;
sortProcess.StartInfo.Arguments = this.comboBox1.SelectedItem.ToString();
//设置UseShellExecute为false重定向。
sortProcess.StartInfo.CreateNoWindow = TRUE;
sortProcess.StartInfo.UseShellExecute = FALSE;



//重定向sort命令的标准输出。
//该物流被读异步使用的事件处理程序。
sortProcess.StartInfo.RedirectStandardOutput = TRUE;
sortOutput =新的StringBuilder();

//设置我们的事件处理异步读取排序输出。
sortProcess.OutputDataReceived + =新DataReceivedEventHandler(SortOutputHandler);

//重定向标准输入,以及。这种流
//进行同步使用。
sortProcess.StartInfo.RedirectStandardInput = TRUE;

//启动过程。
sortProcess.Start();



//开始排序输出流的异步读取。
sortProcess.BeginOutputReadLine();
sortProcess.WaitForExit();


richTextBox1.AppendText(sortOutput.ToString());
}

私有静态无效SortOutputHandler(对象sendingProcess,
DataReceivedEventArgs轮廓)
{
sortOutput.Append(Environment.NewLine +
[+ numOutputLines.ToString()+] - + outLine.Data);
}
}



所以当SORT.EXE启动,它会显示文字,我希望可以同时显示所有文本RichTextBox中的实时(我不想等待进程退出,然后读取所有输出)



我怎样才能做到这一点?我的代码错任何部分?谢谢



更新@botz



我添加了这个在我的代码



 私人无效SortOutputHandler(对象sendingProcess,
DataReceivedEventArgs轮廓)
{

sortOutput.Append(Environment.NewLine +
[+ numOutputLines.ToString()+ - + outLine.Data);
richTextBox1.AppendText(sortOutput.ToString());


}



但引发此异常。



 跨线程操作无效:控制'richTextBox1从比它创建的线程以外的线程访问。 


解决方案

WaitForExit()阻塞你的UI线程,所以你不要看不到新的输出。
或者等待的过程在一个单独的线程或像这样的东西替换 WaitForExit()

  {时
Application.DoEvents()(sortProcess.HasExited!); //这样可以使你的表单响应通过处理事件
}

在你的 SortOutputHandler ,你现在可以直接输出附加到你的文本框。但是你要记得检查是否需要调用它的UI线程。



您可以检查它是否在UI线程上这样在您的处理程序:

 如果(richTextBox1.InvokeRequired){richTextBox1.BeginInvoke(新DataReceivedEventHandler(SortOutputHandler),新[] {sendingProcess,轮廓}); } 
,否则{
sortOutput.Append(Environment.NewLine +[+ numOutputLines.ToString()+ - + outLine.Data);
richTextBox1.AppendText(sortOutput.ToString());
}


what is wrong why is that the richtextbox doesnt get the stream of Process output? theres no text display in richtextbox..

 private void button1_Click(object sender, EventArgs e)
    {

        Process sortProcess;
        sortProcess = new Process();
        sortProcess.StartInfo.FileName = "sort.exe";
        sortProcess.StartInfo.Arguments = this.comboBox1.SelectedItem.ToString();
        // Set UseShellExecute to false for redirection.
        sortProcess.StartInfo.CreateNoWindow = true;
        sortProcess.StartInfo.UseShellExecute = false;



        // Redirect the standard output of the sort command.  
        // This stream is read asynchronously using an event handler.
        sortProcess.StartInfo.RedirectStandardOutput = true;
        sortOutput = new StringBuilder("");

        // Set our event handler to asynchronously read the sort output.
        sortProcess.OutputDataReceived += new DataReceivedEventHandler(SortOutputHandler);

        // Redirect standard input as well.  This stream
        // is used synchronously.
        sortProcess.StartInfo.RedirectStandardInput = true;

        // Start the process.
        sortProcess.Start();



        // Start the asynchronous read of the sort output stream.
        sortProcess.BeginOutputReadLine();
        sortProcess.WaitForExit();


        richTextBox1.AppendText(sortOutput.ToString());
    }

    private static void SortOutputHandler(object sendingProcess,
        DataReceivedEventArgs outLine)
    {
            sortOutput.Append(Environment.NewLine +
                "[" + numOutputLines.ToString() + "] - " + outLine.Data);
        }
    }

so when sort.exe launches, it displays text, i want all those text be displayed also in richtextbox in RealTime (i dont want to wait for the process to exit, and then read all output)

how can i do it? any wrong part of my code? thanks

UPDATE @botz

i added this in my code

 private void SortOutputHandler(object sendingProcess,
        DataReceivedEventArgs outLine)
    {

            sortOutput.Append(Environment.NewLine +
                "[" + numOutputLines.ToString() + "] - " + outLine.Data);
            richTextBox1.AppendText(sortOutput.ToString());


    }

but it throws this exception

Cross-thread operation not valid: Control 'richTextBox1' accessed from a thread other than the thread it was created on.

解决方案

WaitForExit() blocks your UI Thread, so you don't see the new output. Either wait for the process in a separate thread or replace WaitForExit() with something like this:

while (!sortProcess.HasExited) {
     Application.DoEvents(); // This keeps your form responsive by processing events
}

In your SortOutputHandler, you can now directly append output to your textbox. But you should remember to check if you need to invoke it on the UI Thread.

You can check if it's on the UI thread this way in your handler:

    if (richTextBox1.InvokeRequired) { richTextBox1.BeginInvoke(new DataReceivedEventHandler(SortOutputHandler), new[] { sendingProcess, outLine }); }
    else {
        sortOutput.Append(Environment.NewLine + "[" + numOutputLines.ToString() + "] - " + outLine.Data);
        richTextBox1.AppendText(sortOutput.ToString());
    }

这篇关于我如何可以重定向过程输出(控制台)到RichTextBox的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆