2011-05-11 49 views
2

我已经在这个问题上工作了一段时间显示它。我可以捕获控制台窗口的输出(live),但我无法捕获实时中的python控制台应用程序的输出。我可以在运行完成后捕获python程序的输出,但我不想那样。 我使用从system.diagonistics过程。与一名背景工作者。 我只是想捕捉的python26输出到一个文本框。我已经用其他自定义应用程序测试过我的程序,并且它显示输出(实时)。C#捕获python.exe输出,并在文本框

请帮助

感谢

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

您是否试图轮询输出流而不是使用该事件并查看您是否正在创建任何数据。文档反映在每次调用该事件时都会调用该事件,但是我不知道这是否意味着\ c \ n或仅仅是\ n \ n – rerun 2011-05-11 16:59:43

+0

Ironpython文本框可能是您的目的的有用工具http://www.codeproject.com/KB/ miscctrl/irontextbox.aspx – lucemia 2011-05-11 17:13:53

+0

你有没有想出解决办法? – Sugitime 2013-10-31 03:12:34

回答

0

我记得有一个类似的问题了一段时间回来,我觉得我做类似这样的东西在我的.py脚本,而不是使用print功能:

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

不知道我是否将shell参数设置为TrueFalse。也不确定所有的“标准”参数。你可能想在那里尝试一下。

0

如果你开始从你的代码中的Python程序,然后THIS会让你的生活真的容易,我认为这是即将去最彻底的方法。

1

我遇到了这个问题,同时为此目的准备了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(); 
     } 
0

我只是遇到这个问题我自己,一吨的实验,是什么为我工作后,运行与“-u”选项,蟒蛇的过程,使输出无缓冲。有了这一切,一切工作都很好。