Search code examples
c#output-redirect

Windows Form - Running python script, redirected output delayed


I'm running a windows form with a background worker to update a textbox based on the output of a python script. Its all working pretty well, except the redirected output is not in real time; its delayed pretty significantly.

Any ideas how I can increase the redirected outputs response time?

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 JiraHeartBeat
{
    public partial class Form1 : Form
    {

        delegate void AppendTextDelegate(string text);
        BackgroundWorker Worker = new BackgroundWorker();

        public Form1()
        {
            InitializeComponent();

            Worker.DoWork += new DoWorkEventHandler(Worker_DoWork);
            Worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Worker_RunWorkerCompleted);

        }

        void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            StartButton.PerformClick();
        }

        private void StartButton_Click(object sender, EventArgs e)
        {
            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);

            pro.StartInfo.FileName = "C:\\Python27\\python.exe";
            pro.StartInfo.Arguments = "\"C:\\Python27\\myscript.py\"";

            try
            {
                pro.Start();
                pro.BeginOutputReadLine();
                pro.BeginErrorReadLine();
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            Thread.Sleep(5000 * 60);
        }


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

Solution

  • Actually, the issue is that Python does not redirect output until the script is complete. I believe IronPython will redirect while the script is running (have not tested this though), but unfortunately, regular Python must wait for the script to end before redirecting output.