Problem with reading process's output using BeginOutputReadLine

  • Thread starter =?ISO-8859-1?Q?Mikko_Nyl=E9n?=
  • Start date
?

=?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
 
?

=?ISO-8859-1?Q?Mikko_Nyl=E9n?=

import time

i = 1
while(True):
print "Wrote line " + str(i)

i++
time.sleep(5)

I meant to write i += 1 instead of i++. Sorry for the typo.

- Mikko Nylén
 
M

mabra

Hi !

Try:

process.StartInfo.CreateNoWindow = true;

I had a similar proggi and no separate command window;Although, I was
starting the subprocess from a console app.

Best regards,
Manfred
 
?

=?ISO-8859-1?Q?Mikko_Nyl=E9n?=

mabra said:
Hi !

Try:

process.StartInfo.CreateNoWindow = true;

I had a similar proggi and no separate command window;Although, I was
starting the subprocess from a console app.

Best regards,
Manfred

Thanks.

However, the problem with the output of the program not being added to the
RichTextBox still keeps me from finishing the software. If I run the Python
script from the command line, it displays the output in real time, not after
the script finishes.

- Mikko Nylén
 
M

mabra

Hi !

BTW, thanks for your sample ;-)
What was new to me - not seen the .Net 2.0 classes in detail before -
is, that you can async connect to the ouput via:

process.OutputDataReceived += new
DataReceivedEventHandler(OnOutputDataReceived);

I modified one of my sync working proggis to your method and for me it
works fine. I had just a tester, which generates output interrupted by
several sleep's. You method is working fine for me, I get each output
line immidiately, not at the end.

I do not know all the dteails about the MTA/STA threading, but so far I
know, a forms app uses STA. Could this be the reason? Try to create a
separate thread, explicitely specifying MTA and then create the subprocess.

As I sad, I am not very clear about these MTA/STA things, just an idea,
because your shown code works fine for me in a console app!!

Best regards,
Manfred
 
?

=?ISO-8859-1?Q?Mikko_Nyl=E9n?=

mabra said:
Hi !

BTW, thanks for your sample ;-)
What was new to me - not seen the .Net 2.0 classes in detail before -
is, that you can async connect to the ouput via:

process.OutputDataReceived += new
DataReceivedEventHandler(OnOutputDataReceived);

I modified one of my sync working proggis to your method and for me it
works fine. I had just a tester, which generates output interrupted by
several sleep's. You method is working fine for me, I get each output
line immidiately, not at the end.

I do not know all the dteails about the MTA/STA threading, but so far I
know, a forms app uses STA. Could this be the reason? Try to create a
separate thread, explicitely specifying MTA and then create the subprocess.

As I sad, I am not very clear about these MTA/STA things, just an idea,
because your shown code works fine for me in a console app!!

Best regards,
Manfred

Thanks for your reply. I tried to move the code for executing the process to a
another method called executeProcess() and then modified the Form1__Load() to
contain this:

Thread thread = Thread(new ThreadStart(executeProcess));
thread.TrySetApartmentState(ApartmentState.MTA);
thread.Start()

However, this didn't fix the problem.

- Mikko Nylén
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top