?
=?ISO-8859-1?Q?Mikko_Nyl=E9n?=
Hi!,
I'm trying to read the output of an process as it gets executed and
append the output to a RichTextBox.
However, I have a little problem: the process I'm trying to execute is a
Python script, which uses the time.sleep() to sleep for a specified
amount of time. This (the use of time.sleep()) seems to have the side
effect of the output not being added to the RichTextBox until the
process has finished, which it won't do as the process executes until
it's stopped by the user. Without the call to time.sleep(), the program
runs as excepted.
The question is, can this be fixed from C# code, as I really can't
modify the Python script?
The C# code:
private void Form1_Load(object sender, EventArgs e)
{
Process process = new Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.FileName = "python";
process.StartInfo.Arguments = "test.py";
process.OutputDataReceived +=
new DataReceivedEventHandler(process_OutputDataReceived);
process.Start();
process.BeginOutputReadLine();
}
delegate void AddTextCallback(string text);
void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
this.AddText(e.Data);
}
private void AddText(string text)
{
if (this.outputBox.InvokeRequired)
{
AddTextCallback d = new AddTextCallback(AddText);
this.Invoke(d, new object[] { text });
}
else
{
this.outputBox.Text += text + Environment.NewLine;
}
}
The Python script (test.py) used for testing:
import time
i = 1
while(True):
print "Wrote line " + str(i)
i++
time.sleep(5)
Also, I'd like to ask if there is a way to make that command line window
that opens after process.Start() (or I think it opens when Start() is
called) go away?
Thanks,
- Mikko Nylén
I'm trying to read the output of an process as it gets executed and
append the output to a RichTextBox.
However, I have a little problem: the process I'm trying to execute is a
Python script, which uses the time.sleep() to sleep for a specified
amount of time. This (the use of time.sleep()) seems to have the side
effect of the output not being added to the RichTextBox until the
process has finished, which it won't do as the process executes until
it's stopped by the user. Without the call to time.sleep(), the program
runs as excepted.
The question is, can this be fixed from C# code, as I really can't
modify the Python script?
The C# code:
private void Form1_Load(object sender, EventArgs e)
{
Process process = new Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.FileName = "python";
process.StartInfo.Arguments = "test.py";
process.OutputDataReceived +=
new DataReceivedEventHandler(process_OutputDataReceived);
process.Start();
process.BeginOutputReadLine();
}
delegate void AddTextCallback(string text);
void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
this.AddText(e.Data);
}
private void AddText(string text)
{
if (this.outputBox.InvokeRequired)
{
AddTextCallback d = new AddTextCallback(AddText);
this.Invoke(d, new object[] { text });
}
else
{
this.outputBox.Text += text + Environment.NewLine;
}
}
The Python script (test.py) used for testing:
import time
i = 1
while(True):
print "Wrote line " + str(i)
i++
time.sleep(5)
Also, I'd like to ask if there is a way to make that command line window
that opens after process.Start() (or I think it opens when Start() is
called) go away?
Thanks,
- Mikko Nylén