从另一个运行的应用程序读取输出 [英] Reading output from another running application

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

问题描述

我工作在C#中的自定义IDE的脚本语言,我有一个问题。



我试图启动编译器进程(pawncc .EXE)和参数传递给它。我这样做,现在我有一个问题。当我想显示来自编译应用程序的输出,它只是显示它的某些部分。它应该输出这个(得到这个从命令提示符):

 典当编译器3.2.3664版权所有(C)1997-2006,ITB CompuPhase 

newGM.pwn(0):致命错误100:无法从文件读取:包括/ main_include.inc

编译中止。
1错误。



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

 典当编译器3.2 0.3664版权所有(C)1997-2006,ITB CompuPhase 


1错误。



我只是不明白这一点!这是一个非常奇怪的事情。这可能是简单的东西,但我一直在看它,现在研究了好几个小时!这里是我的代码:

 公共Form3(字符串路径)
{
的InitializeComponent();

this._path =路径;

流程myProcess =新工艺();
的ProcessStartInfo的StartInfo =新的ProcessStartInfo(pawncc.exe);
startInfo.CreateNoWindow = TRUE;
startInfo.UseShellExecute = FALSE;
startInfo.RedirectStandardOutput = TRUE;
startInfo.Arguments =路径+-r-D2
myProcess.StartInfo = StartInfo的;
myProcess.Start();

,而(真)
{
串的myString;
字节[]缓冲区=新的字节[512];
变种芳= myProcess.StandardOutput.BaseStream.BeginRead(缓冲液,0,512,NULL,NULL);
ar.AsyncWaitHandle.WaitOne();
变种读取动作= myProcess.StandardOutput.BaseStream.EndRead(AR);
如果(读取动作大于0)
{
的myString = Encoding.ASCII.GetString(缓冲液,0,读取动作);
}
,否则
{
myProcess.WaitForExit();
中断;
}
richTextBox1.Text = myString的;

}

}



!编辑:



它确实与此代码同样的事情:

 公共Form3(字符串路径)
{
的InitializeComponent();

this._path =路径;

流程myProcess =新工艺();
的ProcessStartInfo的StartInfo =新的ProcessStartInfo(pawncc.exe);
startInfo.CreateNoWindow = TRUE;
startInfo.UseShellExecute = FALSE;
startInfo.RedirectStandardOutput = TRUE;
startInfo.RedirectStandardError = TRUE;
startInfo.Arguments =路径+-r-D2
myProcess.StartInfo = StartInfo的;
myProcess.Start();使用(StreamReader的读者= myProcess.StandardOutput)
{
字符串结果

= reader.ReadToEnd();
richTextBox1.Text =结果;
}
}


解决方案

您需要重定向标准错误流,以及:

  startInfo.RedirectStandardError = TRUE; 



编辑:我刚刚回顾了代码,发现你只是只读的StandardOutput流。



我一般监控都使用的过程中,DataReceived事件并添加结果到一个StringBuilder,然后存储在StringBuilder的内容标准和错误输出流的过程UI元素:

 私有静态System.Text.StringBuilder m_sbText; 

公共Form3(字符串路径)
{
的InitializeComponent();

this._path =路径;

流程myProcess =新工艺();
的ProcessStartInfo的StartInfo =新的ProcessStartInfo(pawncc.exe);
startInfo.CreateNoWindow = TRUE;
startInfo.UseShellExecute = FALSE;
startInfo.RedirectStandardOutput = TRUE;
startInfo.RedirectStandardError = TRUE;
startInfo.Arguments =路径+-r-D2
myProcess.StartInfo = StartInfo的;

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

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

myProcess.Start();

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


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

私有静态无效ProcessDataHandler(对象sendingProcess,DataReceivedEventArgs轮廓)
{
//收集net view命令的输出。
如果(!String.IsNullOrEmpty(outLine.Data))
{
//将文本添加到收集输出。
m_sbText.AppendLine(outLine.Data);
}
}

有显然对这个变化,但这应该得到你开始。


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.

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

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