Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#捕获python.exe输出并在文本框中显示_C#_Python_Process_Capture Output - Fatal编程技术网

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

C#捕获python.exe输出并在文本框中显示,c#,python,process,capture-output,C#,Python,Process,Capture Output,我研究这个问题已经有一段时间了。我可以很好地捕获控制台窗口的输出(live),但是我不能实时捕获python控制台应用程序的输出。我可以在python程序完成运行后捕获它的输出,但我不希望这样。 我正在使用system.Diagoritics的过程。有背景工作人员。 我只想将python26的输出捕获到一个文本框中。我已经用其他自定义应用程序测试了我的程序,它确实显示了输出(实时) 请帮忙 谢谢 using System; using System.Collections.Generic; us

我研究这个问题已经有一段时间了。我可以很好地捕获控制台窗口的输出(live),但是我不能实时捕获python控制台应用程序的输出。我可以在python程序完成运行后捕获它的输出,但我不希望这样。 我正在使用system.Diagoritics的过程。有背景工作人员。 我只想将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);
        }
    }

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

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

不确定是否将
shell
参数设置为
True
False
。也不确定所有的“std”参数。您可能需要在那里进行一些试验。

如果您是从代码开始Python过程,那么将使您的生活变得非常简单,我认为这是最干净的方式。

我在做一个完全出于此目的的测试时遇到了这个问题

我用了你的技巧

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

我自己也遇到了这个问题,经过大量的实验,对我有效的方法是使用“-u”选项运行python进程,这使得输出无缓冲。有了这些,一切都很顺利

您是否尝试轮询输出流,而不是使用事件并查看是否正在获取任何数据。文档提到每次收到一行时调用事件,但是我想知道这是否意味着\c\n或只是\nRonPython文本框可能是一个有用的工具。您是否曾经发现这一点?工作完美无瑕!