By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,016 Members | 2,265 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,016 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Mar 6 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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
Mar 6 '07 #2

P: n/a
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().
"Ole" <ol*@blabla.comha scritto nel messaggio
news:%2****************@TK2MSFTNGP03.phx.gbl...
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;o
//.
//.
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


Mar 6 '07 #3

P: n/a
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
"Laura T." <LT@NOWHERE.COMwrote in message
news:O5**************@TK2MSFTNGP04.phx.gbl...
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().
"Ole" <ol*@blabla.comha scritto nel messaggio
news:%2****************@TK2MSFTNGP03.phx.gbl...
>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;o
//.
//.
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



Mar 8 '07 #4

P: n/a
Hans Kesting wrote:
>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)?
The child process's output is the parent process's input.

Arne
Mar 17 '07 #5

P: n/a
Ole wrote:
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
Mar 17 '07 #6

P: n/a
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
Mar 19 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.