A
Al Cohen
I'll start by warning that I'm a newbie to C# (but I've been programming
for 25 years), so I may just be doing something reallyreally dumb.
I'm writing a C# wrapper for a command-line application (pscp.exe, a
secure file-copy app that's part of the excellent PuTTY SSH package).
Getting pscp.exe to run properly was a piece of cake using the
System.Diagnostics.Process class.
The thing that I can't get to work is the ability to read pscp.exe's
standard output on the fly. Every second or so, pscp.exe sends progress
information to the standard output: %completed, estimated time
remaining, and so forth. I'd like to capture this as it happens and
update a progress bar on my Windows app. In principle, this should be
fine since, as I understand things, applications run using
System.Diagnostics.Process are run asynchronously.
To avoid eating all of the CPU time, I set up a scheme using a timer to
periodically fire and read anything new from StandardOutput stream, as
follows:
- Start pscp.exe using Process.Start
- enable timer
- when timer fires:
-- read all new characters in StandardOutput
-- parse the characters for progress information
-- if pscp.exe has exited, disable timer
I've used this type of scheme many times before in real-time programming.
Here's the problem: the first time I call Process.StandardOutput.Read,
my app stops and waits at that line for pscp.exe to exit. By contrast,
if I have the timer just write to a window and test if pscp.exe has
exited (no Process.StandardOutput.Read), all works as expected: the
timer times out periodically and stuff get's written to the screen on
each timeout.
Here is the current version of the offending code segment (I've tried it
a few different ways now, none worked):
public static string UpdateXferStatus()
{
int i;
string incoming = "";
int numCharsRead;
bool endOfInput = false;
while(endOfInput == false)
{
char[] inChars = new char[100];
//freezes at next line until pscp.exe exits
numCharsRead = PProcess.StandardOutput.Read(inChars, 0, 100);
if (numCharsRead > 0)
{
for (i=0; i!=numCharsRead; i++)
{
incoming = incoming + inChars;
}
}
else
{
endOfInput=true;
}
}
return incoming;
}
I've tried two variations of the StandardOutput.Read method, with
similar results.
Any help would be GREATLY appreciated!
Thanks in advance,
Al Cohen
www.alcohen.com
for 25 years), so I may just be doing something reallyreally dumb.
I'm writing a C# wrapper for a command-line application (pscp.exe, a
secure file-copy app that's part of the excellent PuTTY SSH package).
Getting pscp.exe to run properly was a piece of cake using the
System.Diagnostics.Process class.
The thing that I can't get to work is the ability to read pscp.exe's
standard output on the fly. Every second or so, pscp.exe sends progress
information to the standard output: %completed, estimated time
remaining, and so forth. I'd like to capture this as it happens and
update a progress bar on my Windows app. In principle, this should be
fine since, as I understand things, applications run using
System.Diagnostics.Process are run asynchronously.
To avoid eating all of the CPU time, I set up a scheme using a timer to
periodically fire and read anything new from StandardOutput stream, as
follows:
- Start pscp.exe using Process.Start
- enable timer
- when timer fires:
-- read all new characters in StandardOutput
-- parse the characters for progress information
-- if pscp.exe has exited, disable timer
I've used this type of scheme many times before in real-time programming.
Here's the problem: the first time I call Process.StandardOutput.Read,
my app stops and waits at that line for pscp.exe to exit. By contrast,
if I have the timer just write to a window and test if pscp.exe has
exited (no Process.StandardOutput.Read), all works as expected: the
timer times out periodically and stuff get's written to the screen on
each timeout.
Here is the current version of the offending code segment (I've tried it
a few different ways now, none worked):
public static string UpdateXferStatus()
{
int i;
string incoming = "";
int numCharsRead;
bool endOfInput = false;
while(endOfInput == false)
{
char[] inChars = new char[100];
//freezes at next line until pscp.exe exits
numCharsRead = PProcess.StandardOutput.Read(inChars, 0, 100);
if (numCharsRead > 0)
{
for (i=0; i!=numCharsRead; i++)
{
incoming = incoming + inChars;
}
}
else
{
endOfInput=true;
}
}
return incoming;
}
I've tried two variations of the StandardOutput.Read method, with
similar results.
Any help would be GREATLY appreciated!
Thanks in advance,
Al Cohen
www.alcohen.com