从另一个正在运行的应用程序读取输出

Reading output from another running application(从另一个正在运行的应用程序读取输出)
本文介绍了从另一个正在运行的应用程序读取输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I'm working on a custom IDE in C# for a scripting language, and I have a problem.

I'm trying to start the compiler process (pawncc.exe) and pass arguments to it. I've done that, and now I have a problem. When I want to display the output from the compiler application, it only displays some parts of it. It should output this (got this from the command prompt):

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.

But it doesn't. It outputs this (in the application, using the same command/arguments):

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


1 Error.

I just don't get it! It's a really weird thing. It might be something simple but I've been looking at it, and researching for hours now! Here's my code:

        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;

            }

        }

!!EDIT:

It does the same thing with this code:

        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;
            }
        }

解决方案

You need to redirect the standard error stream as well:

startInfo.RedirectStandardError = true;

Edit: I just reviewed the code and discovered that you are only readonly the StandardOutput stream.

I generally monitor the process for both the standard and error output streams using the DataReceived events on the process and adding the results into a stringbuilder, then storing the StringBuilder content in the UI element:

    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);
        }
    }

There are obviously variations on this, but this should get you started.

这篇关于从另一个正在运行的应用程序读取输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)