471,350 Members | 1,842 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

Console app output inside a winform app

I have created a console app that simply prints out a message a couple
times, then exits, here is the code:
<code>
for(int i = 0; i < 10; i++)
{
System.Threading.Thread.Sleep(500);
Console.WriteLine(String.Format("Sleeping...{0}", i));
}
Console.WriteLine("Done!");
</code>
Then in my winform app, I have this bit of code that creates a new Process;
<code>
public void Build()
{
String commandLine = "";
commandLine =
@"C:\PMDRepository\Tools\DQ_Tools\FirmwareEditor\C onsoleTest\bin\Debug\Conso
leTest.exe";

// execute
Process process = new Process();
process.EnableRaisingEvents = true;
process.StartInfo.UseShellExecute = false;
process.Exited += new EventHandler(ProcessExited);
process.StartInfo.FileName = commandLine;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardOutput= true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;

try
{
process.Start();
m_working = true;
string line = string.Empty;
string previousReadLine = string.Empty;
while(process.StandardOutput.Peek() > -1)
{
line = process.StandardOutput.ReadLine();
if(line != previousReadLine)
{
previousReadLine = line;
m_log.LogRaw("IARBuilder:\t{0}", line);
}
}
}
catch(Exception ex)
{
m_log.LogError(ex.Message);
}
}
</code>
in the while(process.StandardOutput.Peek() > -1)) loop, execution enters
that once, but never again. I'm not sure what's happening, but it seems
like it captures the output once, then doesn't try to read it again. Anyone
know the correct way to do this?

Thanks for any help,
Steve
Dec 9 '05 #1
2 4629
I just noticed something interesting.. if I set a breakpoint on line:
line = process.StandardOutput.ReadLine();
and then use F10 to step through the loop, then I can see that each line is
being read and indeed printed to the UI, but if there is no breakpoint only
one line is printed out.

maybe Peek() isn't the best thing to be checking...
"Steve" <ss*@sss.com> wrote in message
news:uC**************@TK2MSFTNGP09.phx.gbl...
I have created a console app that simply prints out a message a couple
times, then exits, here is the code:
<code>
for(int i = 0; i < 10; i++)
{
System.Threading.Thread.Sleep(500);
Console.WriteLine(String.Format("Sleeping...{0}", i));
}
Console.WriteLine("Done!");
</code>
Then in my winform app, I have this bit of code that creates a new Process; <code>
public void Build()
{
String commandLine = "";
commandLine =
@"C:\PMDRepository\Tools\DQ_Tools\FirmwareEditor\C onsoleTest\bin\Debug\Conso leTest.exe";

// execute
Process process = new Process();
process.EnableRaisingEvents = true;
process.StartInfo.UseShellExecute = false;
process.Exited += new EventHandler(ProcessExited);
process.StartInfo.FileName = commandLine;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardOutput= true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;

try
{
process.Start();
m_working = true;
string line = string.Empty;
string previousReadLine = string.Empty;
while(process.StandardOutput.Peek() > -1)
{
line = process.StandardOutput.ReadLine();
if(line != previousReadLine)
{
previousReadLine = line;
m_log.LogRaw("IARBuilder:\t{0}", line);
}
}
}
catch(Exception ex)
{
m_log.LogError(ex.Message);
}
}
</code>
in the while(process.StandardOutput.Peek() > -1)) loop, execution enters
that once, but never again. I'm not sure what's happening, but it seems
like it captures the output once, then doesn't try to read it again. Anyone know the correct way to do this?

Thanks for any help,
Steve

Dec 9 '05 #2
Well, I solved part of this issue, by changing the line:
<code>
while(process.StandardOutput.Peek() > -1)
</code>
to:
<code>
while(m_working == true)
</code>

I was able to get my test console application to work fine, it spit out the
messages just like I would expect.
When I switched over to my real command line app that I want to use, I still
get odd output. When I execute the comman line in a shell, I get this
output:

<console output>
C:\>"C:\Program Files\IAR Systems\Embedded Workbench
4.0\common\bin\iarbuild.exe"
"C:\PMDRepository\IF_Firmware\DuetQuartet.ewp" -build Release
IAR Command Line Build Utility V4.4.2
Copyright 2002-2004 IAR Systems. All rights reserved.
Rebuilding configuration: DuetQuartet - Release
0 file(s) deleted.
ADC.c
Audible.c
Display.c
Flash.c
Ints.s43
KS0066U.c
Key.c
Ports.c
Protocol.c
SPI.c
Timers.c
UI.c
main.c
pmd_strlen.c
Linking
Total number of errors: 0
Total number of warnings: 2
</console output>

When I run the same command line with my Process object, I get this output
in my GUI:
<output>
Copyright 2002-2004 IAR Systems. All rights reserved.
IAR Command Line Build Utility V4.4.2
</output>

So... why aren't I seeing everything else? Any ideas what I could check
for?
"Steve" <ss*@sss.com> wrote in message
news:uN**************@tk2msftngp13.phx.gbl...
I just noticed something interesting.. if I set a breakpoint on line:
line = process.StandardOutput.ReadLine();
and then use F10 to step through the loop, then I can see that each line is being read and indeed printed to the UI, but if there is no breakpoint only one line is printed out.

maybe Peek() isn't the best thing to be checking...
"Steve" <ss*@sss.com> wrote in message
news:uC**************@TK2MSFTNGP09.phx.gbl...
I have created a console app that simply prints out a message a couple
times, then exits, here is the code:
<code>
for(int i = 0; i < 10; i++)
{
System.Threading.Thread.Sleep(500);
Console.WriteLine(String.Format("Sleeping...{0}", i));
}
Console.WriteLine("Done!");
</code>
Then in my winform app, I have this bit of code that creates a new

Process;
<code>
public void Build()
{
String commandLine = "";
commandLine =

@"C:\PMDRepository\Tools\DQ_Tools\FirmwareEditor\C onsoleTest\bin\Debug\Conso
leTest.exe";

// execute
Process process = new Process();
process.EnableRaisingEvents = true;
process.StartInfo.UseShellExecute = false;
process.Exited += new EventHandler(ProcessExited);
process.StartInfo.FileName = commandLine;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardOutput= true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;

try
{
process.Start();
m_working = true;
string line = string.Empty;
string previousReadLine = string.Empty;
while(process.StandardOutput.Peek() > -1)
{
line = process.StandardOutput.ReadLine();
if(line != previousReadLine)
{
previousReadLine = line;
m_log.LogRaw("IARBuilder:\t{0}", line);
}
}
}
catch(Exception ex)
{
m_log.LogError(ex.Message);
}
}
</code>
in the while(process.StandardOutput.Peek() > -1)) loop, execution enters
that once, but never again. I'm not sure what's happening, but it seems
like it captures the output once, then doesn't try to read it again.

Anyone
know the correct way to do this?

Thanks for any help,
Steve


Dec 9 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Boba | last post: by
4 posts views Thread by Ron Vecchi | last post: by
2 posts views Thread by Schorschi | last post: by
4 posts views Thread by =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?= | last post: by
27 posts views Thread by CarlosMB | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.