动态获取命令行输出

16
我正在使用C#命令行运行一个程序,该程序在运行时会产生一些日志,我需要在每次更改时显示这些日志。我编写了以下代码,但是当进程被终止后它会显示所有的日志,并且在运行时我的程序没有响应。我该如何解决?
谢谢!
ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + "C:\\server.py");
Process proc = new Process();
procStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
procStartInfo.UseShellExecute = false;
procStartInfo.RedirectStandardOutput = true;
//procStartInfo.CreateNoWindow = true;
proc.StartInfo = procStartInfo;
proc.Start();
string output = proc.StandardOutput.ReadToEnd();
proc.WaitForExit(300);
LogstextBox.Text = output;

编辑: 我尝试使用OutputDataReceived,但没有显示任何结果,以下是更改后的代码:

{
            //processCaller.FileName = @"ping";
            //processCaller.Arguments = "4.2.2.4 -t"; this is working
            processCaller.FileName = @"cmd.exe";
            processCaller.Arguments = "/c c:\\server.py"; //this is not working
            processCaller.StdErrReceived += new DataReceivedHandler(writeStreamInfo);
            processCaller.StdOutReceived += new DataReceivedHandler(writeStreamInfo);
            processCaller.Completed += new EventHandler(processCompletedOrCanceled);
            processCaller.Cancelled += new EventHandler(processCompletedOrCanceled);
            this.richTextBox1.Text = "Server Started.." + Environment.NewLine;
            processCaller.Start();
    }

    private void writeStreamInfo(object sender, DataReceivedEventArgs e)
    {
        this.richTextBox1.AppendText(e.Text + Environment.NewLine);
    }

请给那些帮助过你的人点赞/接受答案,这是表达感谢之情的方式。 - Chimera
2个回答

22

这是问题:

string output = proc.StandardOutput.ReadToEnd();

只有进程终止后,才能到达标准输出的“末尾”。

您应该一次读取一行 - 或者可能只需订阅OutputDataReceived事件(并遵循该事件的其他文档要求)。

编辑:这是适用于我的示例代码:

using System;
using System.Diagnostics;
using System.Threading;

class Program
{
    public static void Main()
    {
        ProcessStartInfo startInfo = new ProcessStartInfo("cmd", "/c " + "type Test.cs")
        {
            WindowStyle = ProcessWindowStyle.Hidden,
            UseShellExecute = false,
            RedirectStandardOutput = true,
            CreateNoWindow = true
        };

        Process process = Process.Start(startInfo);
        process.OutputDataReceived += (sender, e) => Console.WriteLine(e.Data);
        process.BeginOutputReadLine();
        process.WaitForExit();
        // We may not have received all the events yet!
        Thread.Sleep(5000);
    }
}

注意,在您的示例代码中,您正在访问调用OutputDataReceived处理程序的任何线程上的UI - 对我来说,这看起来像是一个坏主意。

1
你比我先完成了。不意外。 - Chimera
谢谢,我尝试使用OutputDataReceived,但无法弄清楚我做错了什么,因为它不起作用。 - HadiRj
@Th3_c0d3r:好的,你按照文档中的指示操作了吗?特别是,你是否调用了 BeginOutputReadLine 函数? - Jon Skeet
@Th3_c0d3r:我刚刚编辑了我的答案,加入了一些对我有效的代码... - Jon Skeet
@Th3_c0d3r:你确定它是将内容输出到标准输出流而不是标准错误流吗?我很难诊断一个无法轻易复现的问题... - Jon Skeet
显示剩余7条评论

3
你可以使用Process.BeginOutputReadLine方法。该链接展示了一个完整的C#工作示例,使用了OutputDataReceived事件。那个代码示例应该能满足你的需求。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接