C # capturing python.exe output and displaying it in a text box

I have been working on this issue for a while. I can capture the live output in the console window just fine, but I cannot capture the output of the python console application in real time . I can capture the output of a python program after it finishes, but I don't want this. I am using the process from system.diagonistics. with background worker. I just want to write python26 output to a text box. I tested my program with other user applications and it displays output (live).

help me 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); } } 
+2
python c # process
source share
4 answers

I ran into this problem creating MiniConsole specifically for this 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 obtained from ErrorDataReceived instead of OutputDataReceived (with valid commands).

So, I think you are missing:

 pro.BeginErrorReadLine(); 

Also I started the process in the main thread (I have no 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(); } 
+1
source share

I just ran into this question myself, and after a ton of experiments that worked for me, a python process was launched with the -u option, which makes the output unbuffered. In this case, everything worked perfectly fine.

+1
source share

I remember that I had a similar problem, and I think I did something similar to 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 . Also not sure about all the parameters of "std". You might want to experiment a bit.

0
source share

If you start the Python process with your code, THIS will make your life very easy and I think this is the cleanest way to go.

0
source share

All Articles