I'm trying to create a control which is implementing a python console. Unfortunately it doesn't receive any Python output.
Queue<string> qstring = new Queue<string>();
Thread thread;
Process prc;
void pythonmain()
{
if (DesignMode) return;
prc = new Process();
prc.StartInfo.FileName = "python";
prc.StartInfo.Arguments = " -u";
prc.StartInfo.RedirectStandardInput = true;
prc.StartInfo.RedirectStandardOutput = true;
prc.StartInfo.RedirectStandardError = true;
prc.StartInfo.UseShellExecute = false;
prc.StartInfo.CreateNoWindow = true;
prc.EnableRaisingEvents = true;
prc.OutputDataReceived += new DataReceivedEventHandler(prc_OutputDataReceived);
prc.ErrorDataReceived += new DataReceivedEventHandler(prc_ErrorDataReceived);
prc.SynchronizingObject = this;
prc.Start();
prc.BeginOutputReadLine();
prc.BeginErrorReadLine();
while (!prc.HasExited)
{
lock (qstring)
{
if (qstring.Count > 0) prc.StandardInput.Write(qstring.Dequeue()+"\n");
}
}
prc.WaitForExit();
}
void prc_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data != null)
{
richTextBox1.BeginInvoke(new Action<string>((s) => richTextBox1.AppendText(s)),e.Data);
}
}
void prc_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
System.Diagnostics.Process p = (Process)sender;
if (e.Data != null)
{
richTextBox1.BeginInvoke(new Action<string>((s) => richTextBox1.AppendText(s)), e.Data);
}
}
public PyConsoleControl()
{
InitializeComponent();
if (!DesignMode)
{
thread = new Thread(pythonmain);
thread.IsBackground = true;
thread.Start();
}
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
lock (qstring)
{
qstring.Enqueue(textBox1.Text);
}
textBox1.Clear();
}
}
}
How you can see I'm creating a new thread which is starting a Python shell, but no Python output is returned. How can I fix it?