Console app output inside a winform app

S

Steve

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\ConsoleTest\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
 
S

Steve

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...
 
S

Steve

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?
 

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