C# 捕获 python.exe 输出并将其显示在文本框中

发布于 2024-11-06 17:29:16 字数 3065 浏览 6 评论 0原文

我已经研究这个问题有一段时间了。我可以很好地捕获控制台窗口的输出(实时),但无法实时捕获 python 控制台应用程序的输出。我可以在 python 程序完成运行后捕获它的输出,但我不希望这样。 我正在使用 system.diagonistics 中的流程。与后台工作者。 我只是想将 python26 输出捕获到文本框中。我已经使用其他自定义应用程序测试了我的程序,并且它确实显示了输出(实时)。

请帮忙

谢谢

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;
using System.IO;

namespace ProcessDisplayoutput
{
public partial class Form1 : Form
{

    //Delegates

    delegate void AppendTextDelegate(string text);

    public Form1()
    {
        InitializeComponent();
        Worker.DoWork += new DoWorkEventHandler(Worker_DoWork);

    }

    private void StartButton_Click(object sender, EventArgs e)
    {
        ResultTextBox.Clear();
        if (!Worker.IsBusy)
        {
            Worker.RunWorkerAsync();
        }
    }

    public void Worker_DoWork(object sender, DoWorkEventArgs e)
    {
        Process pro = new Process();
        pro.StartInfo.RedirectStandardOutput = true;
        pro.StartInfo.RedirectStandardError = true;
        pro.StartInfo.UseShellExecute = false;
        pro.StartInfo.CreateNoWindow = true;
        pro.EnableRaisingEvents = true;
        pro.OutputDataReceived +=new DataReceivedEventHandler(OnDataReceived);
        pro.ErrorDataReceived +=new DataReceivedEventHandler(OnDataReceived);

        //Test with random program worked,
        //now need to test with python
        //*****************TEST 1: PASSED **************************
        pro.StartInfo.FileName = "C:\\TestProcessOutput.exe";
        //*****************END TEST1*******************************

        //*****************TEST 2: FAILED *************************
        //pro.StartInfo.FileName = "C:\\Python26\\python.exe";
        //pro.StartInfo.Arguments = "\"C:\\Python26\\testScript.py\"";
        //*****************END TEST2 *******************************

        StreamReader sr = null;
        try
        {
            pro.Start();

            pro.BeginOutputReadLine();
            //An alternative option to display the output with the same results
            //sr = pro.StandardOutput;
            //string line = "";
            //while ((line = sr.ReadLine()) != null)
            //{
           //     appendText(line);
           // }

        }

        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }


    public void OnDataReceived(object sender, DataReceivedEventArgs e)
    {

        if (e.Data != null)
        {
            string temp = (e.Data) + Environment.NewLine;
            appendText(temp);

        }
    }
    public void appendText(string text)
    {
        if (ResultTextBox.InvokeRequired)
        {
            ResultTextBox.Invoke(new AppendTextDelegate(appendText), new object[] { text });
        }
        else
        {
            ResultTextBox.AppendText(text);
        }
    }

I have worked on this issue for a while. I can capture the output(live) of the console window just fine, but I can't capture the output of a python console application in real time. I can capture the output of the python program after it has finished running, but i don't want that.
I am using process from system.diagonistics. with a background worker.
I simply want to capture the python26 output onto a text box. I have tested my program with other custom applications, and it does display the output(live).

Help please

Thanks

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;
using System.IO;

namespace ProcessDisplayoutput
{
public partial class Form1 : Form
{

    //Delegates

    delegate void AppendTextDelegate(string text);

    public Form1()
    {
        InitializeComponent();
        Worker.DoWork += new DoWorkEventHandler(Worker_DoWork);

    }

    private void StartButton_Click(object sender, EventArgs e)
    {
        ResultTextBox.Clear();
        if (!Worker.IsBusy)
        {
            Worker.RunWorkerAsync();
        }
    }

    public void Worker_DoWork(object sender, DoWorkEventArgs e)
    {
        Process pro = new Process();
        pro.StartInfo.RedirectStandardOutput = true;
        pro.StartInfo.RedirectStandardError = true;
        pro.StartInfo.UseShellExecute = false;
        pro.StartInfo.CreateNoWindow = true;
        pro.EnableRaisingEvents = true;
        pro.OutputDataReceived +=new DataReceivedEventHandler(OnDataReceived);
        pro.ErrorDataReceived +=new DataReceivedEventHandler(OnDataReceived);

        //Test with random program worked,
        //now need to test with python
        //*****************TEST 1: PASSED **************************
        pro.StartInfo.FileName = "C:\\TestProcessOutput.exe";
        //*****************END TEST1*******************************

        //*****************TEST 2: FAILED *************************
        //pro.StartInfo.FileName = "C:\\Python26\\python.exe";
        //pro.StartInfo.Arguments = "\"C:\\Python26\\testScript.py\"";
        //*****************END TEST2 *******************************

        StreamReader sr = null;
        try
        {
            pro.Start();

            pro.BeginOutputReadLine();
            //An alternative option to display the output with the same results
            //sr = pro.StandardOutput;
            //string line = "";
            //while ((line = sr.ReadLine()) != null)
            //{
           //     appendText(line);
           // }

        }

        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }


    public void OnDataReceived(object sender, DataReceivedEventArgs e)
    {

        if (e.Data != null)
        {
            string temp = (e.Data) + Environment.NewLine;
            appendText(temp);

        }
    }
    public void appendText(string text)
    {
        if (ResultTextBox.InvokeRequired)
        {
            ResultTextBox.Invoke(new AppendTextDelegate(appendText), new object[] { text });
        }
        else
        {
            ResultTextBox.AppendText(text);
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

拧巴小姐 2024-11-13 17:29:16

我自己刚刚遇到了这个问题,经过大量的实验,对我有用的是使用“-u”选项运行 python 进程,这使得输出无缓冲。就这样,一切都工作得很好。

I just ran into this question myself, and after a ton of experimenting, what worked for me was running the python process with the "-u" option, which makes the output unbuffered. With that, everything worked completely fine.

维持三分热 2024-11-13 17:29:16

我在专门为此目的制作 MiniConsole 时遇到了这个问题。

我使用了你的技术

pro.EnableRaisingEvents = true;
pro.OutputDataReceived +=new DataReceivedEventHandler(OnDataReceived);
pro.ErrorDataReceived +=new DataReceivedEventHandler(OnDataReceived);

奇怪的是,所有输出都来自 ErrorDataReceived 而不是 OutputDataReceived (带有有效命令)。

所以我认为你错过了:

pro.BeginErrorReadLine();

此外,我正在使用 python27 在主线程中启动该进程(我没有任何工作人员)。

这是完整的开始:

        // executable: "c:\\python27\\python.exe", arguments: "myscript.py"
        ProcessStartInfo startInfo = new ProcessStartInfo(executable, arguments);
        startInfo.CreateNoWindow = true;
        startInfo.UseShellExecute = false;
        startInfo.RedirectStandardOutput = true;
        startInfo.RedirectStandardError = true;
        startInfo.WindowStyle = ProcessWindowStyle.Hidden;
        startInfo.WorkingDirectory = textBoxWorkingDirectory.Text;

        try
        {
            Process p = new Process();
            p.StartInfo = startInfo;
            p.EnableRaisingEvents = true;
            p.OutputDataReceived += new DataReceivedEventHandler(OnDataReceived);
            p.ErrorDataReceived += new DataReceivedEventHandler(OnDataReceived);
            p.Exited += new EventHandler(OnProcessExit);
            p.Start();
            p.BeginOutputReadLine();
            p.BeginErrorReadLine();
        }

I ran into this problem while making a MiniConsole exactly for that purpose.

I used your technique with

pro.EnableRaisingEvents = true;
pro.OutputDataReceived +=new DataReceivedEventHandler(OnDataReceived);
pro.ErrorDataReceived +=new DataReceivedEventHandler(OnDataReceived);

The strange thing is that all the output was coming from ErrorDataReceived instead of OutputDataReceived (with valid commands).

So I think you're missing:

pro.BeginErrorReadLine();

Also I was starting the process in the main thread (I don't have any worker), using python27.

Here is the full start:

        // executable: "c:\\python27\\python.exe", arguments: "myscript.py"
        ProcessStartInfo startInfo = new ProcessStartInfo(executable, arguments);
        startInfo.CreateNoWindow = true;
        startInfo.UseShellExecute = false;
        startInfo.RedirectStandardOutput = true;
        startInfo.RedirectStandardError = true;
        startInfo.WindowStyle = ProcessWindowStyle.Hidden;
        startInfo.WorkingDirectory = textBoxWorkingDirectory.Text;

        try
        {
            Process p = new Process();
            p.StartInfo = startInfo;
            p.EnableRaisingEvents = true;
            p.OutputDataReceived += new DataReceivedEventHandler(OnDataReceived);
            p.ErrorDataReceived += new DataReceivedEventHandler(OnDataReceived);
            p.Exited += new EventHandler(OnProcessExit);
            p.Start();
            p.BeginOutputReadLine();
            p.BeginErrorReadLine();
        }
将军与妓 2024-11-13 17:29:16

我记得不久前遇到过类似的问题,我想我在 .py 脚本中做了类似的事情,而不是使用 print 函数:

sLog = 'Hello World!'
subprocess.Popen( 'echo ' + sLog, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True )

不确定是否设置了 shell 参数为 TrueFalse 。也不确定所有“std”参数。您可能想在那里进行一些实验。

I remember having a similar issue a while back and I think I did something similar to this in my .py scripts instead of using the print function:

sLog = 'Hello World!'
subprocess.Popen( 'echo ' + sLog, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True )

Not sure if I set the shell parameter to True or False though. Also not sure about all the "std" parameters. You might want to experiment a bit there.

維他命╮ 2024-11-13 17:29:16

如果您从代码启动 Python 进程,则 THIS 会让你的生活真正变得轻松,我认为这是最干净的方式 去。

If you're starting the Python process from your code, then THIS will make your life really easy and I think it's about the cleanest way to go.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文