1

私はしばらくこの問題に取り組んできました。コンソール ウィンドウの出力 (ライブ) は問題なくキャプチャできますが、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);
        }
    }
4

4 に答える 4

1

まさにその目的のためにMiniConsoleを作成しているときに、この問題に遭遇しました。

私はあなたのテクニックを使った

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

奇妙なことは、すべての出力が (有効なコマンドを使用して) OutputDataReceived ではなく ErrorDataReceived から来ていたことです。

だから私はあなたが行方不明だと思います:

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();
        }
于 2013-03-02T17:59:26.833 に答える
0

printしばらく前に同様の問題があったことを覚えています。関数を使用する代わりに、.py スクリプトでこれと同様のことをしたと思います。

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

shellパラメータをTrueorに設定したかどうかはわかりませんFalse。また、すべての「std」パラメーターについてもわかりません。そこで少し実験してみるといいかもしれません。

于 2011-05-11T17:09:26.900 に答える
0

コードからPythonプロセスを開始する場合、これにより作業が非常に簡単になります。これが最もクリーンな方法だと思います。

于 2011-05-11T18:05:49.187 に答える