StandardOutput.ReadLine() doesn't work until the app exits???

O

Ole

Hi,

I'm running a command line process from my C# application trying to catch
the output messages from the process into a textbox in my windows form. But
the text doesn't update (the ReadLine doesn't return) until the process
exits??? Any help is highly appreciated! The code is like this:

Process myProcess = new Process();
myProcess.StartInfo.FileName = Application.StartupPath +
"\\myCmdLine.exe";
myProcess.StartInfo.WorkingDirectory = Application.StartupPath;
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.CreateNoWindow = true;
myProcess.StartInfo.RedirectStandardOutput = true;
Thread processThread;
//.
//.
private void buttonStartProcess_Click(object sender, EventArgs e)
{
processThread = new Thread(new ThreadStart(runProcess));
processThread.IsBackground = true;
processThread.Start();
}

private void runProcess()
{
string data;
myProcess.Start();
while ((data = myProcess.StandardOutput.ReadLine()) != null)
{
UpdateTextBox(textBoxMessage, data); // this is a call to an
invoke function
}
}

Thanks,
Ole
 
H

Hans Kesting

Hi,
I'm running a command line process from my C# application trying to
catch the output messages from the process into a textbox in my
windows form. But the text doesn't update (the ReadLine doesn't
return) until the process exits??? Any help is highly appreciated! The
code is like this:
while ((data = myProcess.StandardOutput.ReadLine()) !=
null)

Are you sure you want Standard *Output*, and not StandardInput (or whatever
it's called)?

Hans Kesting
 
L

Laura T.

Many things can be the cause of this.
ReadLine returns after it gets CR/LF that it might not be seeing (When the
child process closes, it closes the stream too and thus the ReadLine
completes anyway).
The myCmdLine.exe's console buffer could not be filled enough until exit
(normal buffer is 4k).
If the myCmdLine.exe is yours, you could add some Console.Out.Flush() calls.
Also be aware that synchronous reading from child process can produce
deadlocks. You'd better of with BeginOutputReadLine (check documentation for
that) or, use Read() or ReadToEnd().
 
O

Ole

Thanks!

I couldn't get it to work no matter what I did, so i guess that the command
line application doesn't output the stream until it has finished. I'll have
to live with that a command window opens and closes during my program run.

Thanks
Ole
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Hans said:
Are you sure you want Standard *Output*, and not StandardInput (or
whatever it's called)?

The child process's output is the parent process's input.

Arne
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Ole said:
I couldn't get it to work no matter what I did, so i guess that the command
line application doesn't output the stream until it has finished. I'll have
to live with that a command window opens and closes during my program run.

Flush should fix it.

Note that while output traditionally is buffered then error
is not.

Sometimes it is just easy to write to error.

Arne
 
O

Ole

Flush should fix it.
Note that while output traditionally is buffered then error
is not.

Thanks, but I don't have control over the command line application - it's a
third party one.

BR
Ole
 

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