2011-05-11 6 views
2

저는 잠시 동안이 문제를 해결했습니다. 콘솔 창 출력 (live)을 캡처 할 수는 있지만,에 파이썬 콘솔 응용 프로그램의 출력을 캡처 할 수 없습니다. 실행이 끝난 후에 파이썬 프로그램의 결과물을 캡처 할 수는 있지만 원하는 것은 아닙니다. system.diagonistics의 프로세스를 사용하고 있습니다. 배경 작업자와. 파이썬 26 출력을 텍스트 상자에 캡처하기 만하면됩니다. 내 프로그램을 다른 사용자 지정 응용 프로그램과 함께 테스트 한 결과 출력을 표시합니다 (라이브).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을 의미하는지 궁금하다. – rerun

+0

Ironpython 텍스트 박스는 아마도 당신의 목적에 유용한 도구 일 것이다. http://www.codeproject.com/KB/ miscctrl/irontextbox.aspx – lucemia

+0

혹시 이것을 알아 냈습니까? – Sugitime

답변

관련 문제