【问题标题】:Reading output from another running application从另一个正在运行的应用程序读取输出
【发布时间】:2012-12-16 10:47:05
【问题描述】:

我正在使用 C# 编写用于脚本语言的自定义 IDE,但我遇到了问题。

我正在尝试启动编译器进程 (pawncc.exe) 并向其传递参数。我已经这样做了,现在我遇到了问题。当我想显示编译器应用程序的输出时,它只显示其中的一部分。它应该输出这个(从命令提示符得到这个):

Pawn compiler 3.2.3664                  Copyright (c) 1997-2006, ITB CompuPhase

newGM.pwn(0) : fatal error 100: cannot read from file: "includes/main_include.inc"

Compilation aborted.
1 Error.

但事实并非如此。它输出这个(在应用程序中,使用相同的命令/参数):

Pawn compiler 3.2.3664          Copyright (c) 1997-2006, ITB CompuPhase


1 Error.

我就是不明白!这真是一件很奇怪的事情。这可能很简单,但我一直在研究它,并且研究了几个小时!这是我的代码:

        public Form3(string path)
        {
            InitializeComponent();

            this._path = path;

            Process myProcess = new Process();
            ProcessStartInfo startInfo = new ProcessStartInfo("pawncc.exe");
            startInfo.CreateNoWindow = true;
            startInfo.UseShellExecute = false;
            startInfo.RedirectStandardOutput = true;
            startInfo.Arguments = path + " -r -d2";
            myProcess.StartInfo = startInfo;
            myProcess.Start();

            while (true)
            {
                string myString;
                byte[] buffer = new byte[512];
                var ar = myProcess.StandardOutput.BaseStream.BeginRead(buffer, 0, 512, null, null);
                ar.AsyncWaitHandle.WaitOne();
                var bytesRead = myProcess.StandardOutput.BaseStream.EndRead(ar);
                if (bytesRead > 0)
                {
                    myString = Encoding.ASCII.GetString(buffer, 0, bytesRead);
                }
                else
                {
                    myProcess.WaitForExit();
                    break;
                }
                richTextBox1.Text = myString;

            }

        }

!!编辑:

这段代码做同样的事情:

        public Form3(string path)
        {
            InitializeComponent();

            this._path = path;

            Process myProcess = new Process();
            ProcessStartInfo startInfo = new ProcessStartInfo("pawncc.exe");
            startInfo.CreateNoWindow = true;
            startInfo.UseShellExecute = false;
            startInfo.RedirectStandardOutput = true;
            startInfo.RedirectStandardError = true;
            startInfo.Arguments = path + " -r -d2";
            myProcess.StartInfo = startInfo;
            myProcess.Start();

            using (StreamReader reader = myProcess.StandardOutput)
            {
                string result = reader.ReadToEnd();
                richTextBox1.Text = result;
            }
        }

【问题讨论】:

  • 几天前我刚问过similar question。简短的回答是处理数据事件(myProcess.OutputDataReceived 和myProcess.ErrorDataReceived )远比使用同步读取方法可靠。

标签: c# winforms command output


【解决方案1】:

您还需要重定向标准错误流:

startInfo.RedirectStandardError = true;

编辑:我刚刚查看了代码,发现您只是只读的 StandardOutput 流。

我通常使用流程上的 DataReceived 事件监控标准和错误输出流的流程,并将结果添加到 stringbuilder 中,然后将 StringBuilder 内容存储在 UI 元素中:

    private static System.Text.StringBuilder m_sbText;

    public Form3(string path)
    {
        InitializeComponent();

        this._path = path;

        Process myProcess = new Process();
        ProcessStartInfo startInfo = new ProcessStartInfo("pawncc.exe");
        startInfo.CreateNoWindow = true;
        startInfo.UseShellExecute = false;
        startInfo.RedirectStandardOutput = true;
        startInfo.RedirectStandardError = true;
        startInfo.Arguments = path + " -r -d2";
        myProcess.StartInfo = startInfo;

        m_sbText = new System.Text.StringBuilder(1000);

        myProcess.OutputDataReceived += ProcessDataHandler;
        myProcess.ErrorDataReceived += ProcessDataHandler;

        myProcess.Start();

        myProcess.BeginOutputReadLine();
        myProcess.BeginErrorReadLine();

        while (!myProcess.HasExited)
        {
            System.Threading.Thread.Sleep(500);
            System.Windows.Forms.Application.DoEvents();
        }
        RichTextBox1.Text = m_sbText.ToString();
    }

    private static void ProcessDataHandler(object sendingProcess, DataReceivedEventArgs outLine)
    {
        // Collect the net view command output. 
        if (!String.IsNullOrEmpty(outLine.Data))
        {
            // Add the text to the collected output.
            m_sbText.AppendLine(outLine.Data);
        }
    }

这显然有不同的变化,但这应该可以帮助您入门。

【讨论】:

  • @QuinSchurman:我刚刚意识到您只是在监视 StandardOutput 流。我已经更新了答案以显示使用流程事件接收传入数据,我认为这更容易使用。
  • 非常感谢!有效。标记为答案。另外,感谢您的快速回复。
  • 所以我使用了这段代码——它工作得很好,但我想通过拆分输出并将它们排序到一个列表中来扩展它。例如,我可以将带有行号的错误放入 UI 中的列表中,使输出更易于阅读。更好的例子:将这个newGM.pwn(0) : fatal error 100: cannot read from file: "includes/main_include.inc"改成这个(在newGM的选项卡中,底部的停靠列表。)Errors: %i. error id 100: (FATAL!) file "includes/main_include.inc" is missing or unreadable.
  • 有两次调用Process.Start。
【解决方案2】:

我没有 pawnCC 应用程序,因此无法尝试,但似乎它们将调试信息的详细程度限制在外部应用程序中 - 除了命令提示符。

您可以尝试通过 cmd 生成 pawncc.exe:

"cmd.exe \c CommandParameterToLaunchPawnCCwithArguments"

【讨论】:

  • 不错的主意,因为这会给你“原始”输出
【解决方案3】:

在过去处理衍生进程的原始输出/错误流时,我注意到了一些零星的问题,因此我通常通过事件处理捕获的输出:

Process myProcess = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo("pawncc.exe");
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.Arguments = path + " -r -d2";
myProcess.EnableRaisingEvents = true;
myProcess.OutputDataReceived += OnOutputDataReceived;
myProcess.ErrorDataReceived += OnErrorDataReceived;
myProcess.StartInfo = startInfo;
myProcess.Start();
myProcess.BeginOutputReadLine();
myProcess.BeginErrorReadLine(); 

【讨论】:

  • 这并不能解释为什么检索到了一些输出但不是错误。
  • @JeremyThompson 是的,但我注意到过去在处理衍生进程(processii?)的原始输出/错误流时会出现一些零星问题,因此建议(以及包含 StandardError项目)
  • +1 公平的电话,可能在你的回答中添加这个I've noticed some sporadic issues when dealing with the raw output/error streams from spawned processes in the past, hence my suggestion to hook up the StandardError events.
猜你喜欢
  • 2017-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多