简体   繁体   English

C#将运行流程标准化

[英]C# get running processes standard out

I have an app that runs a background process and I need to show that app's standard out during execution, like real time. 我有一个运行后台进程的应用程序,我需要在执行过程中像实时显示该应用程序的标准。 I tried process.OutputDataReceived but it triggered after background process terminated itself. 我尝试了process.OutputDataReceived但是它在后台进程自身终止后触发。 But I need to show standard output when that out is created by the process. 但是,当流程创建输出时,我需要显示标准输出。

FYI: I redirected standard output and set UseShellExecute false 仅供参考:我重定向了标准输出,并将UseShellExecute设置UseShellExecute false

Here is full changes I did to the process: 这是我对该过程所做的全部更改:

ProcessStartInfo t = new ProcessStartInfo();
t.CreateNoWindow = true;
t.FileName = name;
t.Arguments = args;
t.UseShellExecute = false;
t.CreateNoWindow = true;
t.RedirectStandardOutput = true;
t.RedirectStandardError = true;

Process p = new Process();
p.StartInfo = t;
p.OutputDataReceived += P_OutputDataReceived;
p.Start();
p.BeginOutputReadLine();

And my event: 我的活动:

private void P_OutputDataReceived(object sender, DataReceivedEventArgs e)
    {
        Textbox1.Text = e.Data;
    }

Also I tried this with a python file, an executable version of it, and a C files executable, but same result at all of them. 我还尝试了一个python文件,它的可执行版本和C文件可执行文件,但是在所有它们上都得到了相同的结果。

Edit: I tried this solution but got the same result again 编辑:我尝试解决方案,但再次得到相同的结果

**Update 04.06.2018: **I just found that the P_OutputDataReceived event is also triggeredn when app waits for standart input, but still cannot found a way to trigger that event in real-time **更新04.06.2018:**我刚刚发现当应用程序等待标准输入时也会触发P_OutputDataReceived事件,但仍然找不到实时触发该事件的方法

As the executed process is also a different thread, you need to return to the UI thread for displaying data from that process. 由于执行的进程也是一个不同的线程,因此您需要返回到UI线程以显示该进程中的数据。 This can be achieved with the Invoke method as shown below: 可以通过如下所示的Invoke方法来实现:

private delegate void ShowDataDelegate(string data);

private void P_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
   ShowDataDelegate showDataDelegate = new ShowDataDelegate(ShowData);
   Invoke(showDataDelegate, e.Data);
}

private void ShowData(string data)
{
   textBox1.Text = data;
}

My first answer was wrong and useless. 我的第一个答案是错误且无用的。 Sorry about that. 对于那个很抱歉。 This is a Windows Forms example for how to invoke the application (Robocopy.exe) with a button press and redirect the output to a TextBox on the main form in real time. 这是一个Windows Forms示例,说明如何通过按按钮调用应用程序(Robocopy.exe),并将输出实时重定向到主窗体上的TextBox。

using System;
using System.Diagnostics;
using System.Windows.Forms;

namespace testApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (Process tProc = new Process())
            {
                ProcessStartInfo t = new ProcessStartInfo();
                t.CreateNoWindow = true;
                t.FileName = Environment.SystemDirectory + "\\robocopy.exe";
                t.Arguments = "/?";
                t.UseShellExecute = false;
                t.CreateNoWindow = true;
                t.RedirectStandardOutput = true;
                t.RedirectStandardError = true;

                Process p = new Process();
                p.StartInfo = t;
                p.OutputDataReceived += new DataReceivedEventHandler(P_OutputDataReceived);
                p.Start();
                p.BeginOutputReadLine();                  
            }
        }

        private void P_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            try
            {
                Trace.WriteLine(e.Data);
                this.BeginInvoke(new MethodInvoker(() =>
                {
                    textBox1.AppendText(e.Data ?? string.Empty);
                }));
            }
            catch (System.Exception x)
            {
                MessageBox.Show(x.ToString());
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM