473,480 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Process.StandardOutput.Read Blocks Asynchronous Behavior?

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[i];
}
}
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

Nov 15 '05 #1
3 10361
Thanks Michael!

I'll try launching a second thread to monitor StandardOutput. The MS
docs are a pretty vague (or I'm pretty dense), but they seem to indicate
that new threads are needed only for reading both StandardOutput and
StandardError simultaneously. I'm only monitoring StandardOutput.

Best regards,

Al Cohen

Michael Mayer wrote:
I wrote an article on that exact topic. Here you go:
http://www.mag37.com/csharp/articles/LaunchProcess/
or on codeproject:
http://www.codeproject.com/useritems/LaunchProcess.asp


Nov 15 '05 #2
Tried launching a seperate thread for reading the standard output - this
thread hangs until the DOS executable completes, at which point the
entire standard output for the run is available from StandardOutput.

So, I still can't do what I want.

Is it possible that the DOS executable is doing something naughty?

Thanks,

Al Cohen

Al Cohen wrote:
Thanks Michael!

I'll try launching a second thread to monitor StandardOutput. The MS
docs are a pretty vague (or I'm pretty dense), but they seem to indicate
that new threads are needed only for reading both StandardOutput and
StandardError simultaneously. I'm only monitoring StandardOutput.

Best regards,

Al Cohen

Michael Mayer wrote:
I wrote an article on that exact topic. Here you go:
http://www.mag37.com/csharp/articles/LaunchProcess/
or on codeproject:
http://www.codeproject.com/useritems/LaunchProcess.asp


Nov 15 '05 #3
"Al Cohen" <am***@no.junk.c-o-r-n-e-l-l.edu> wrote in message
news:9r********************@speakeasy.net...
Tried launching a seperate thread for reading the standard output - this
thread hangs until the DOS executable completes, at which point the
entire standard output for the run is available from StandardOutput.

So, I still can't do what I want.

Is it possible that the DOS executable is doing something naughty?


Sounds odd indeed.

Are you still using StandardOutput.Read ( )? Have you tried ReadLine? I'm
not an expert on StreamReader's so I can't give you advice on the subtle
details of how Read works.

Now that you have a separate thread for reading, you should be able to block
on ReadLine(), and do something everytime you get a complete line.

I would try hard to narrow it down to your C# app or the executable you are
running. I might suggest (if you haven't alreayd) downloading my code
from:
http://www.codeproject.com/useritems/LaunchProcess.asp

There is a small ConsoleApp in there that you can run from your C# app to
see if you are correctly capturing the standard out. (You can modify this
one to sleep between output statements, etc). Or find some other console
app and try running it.

You can also try to exectute your dos executable from my C# program. See if
both / either of these work.

I suppose it's possible that an application could cache it's standard out
and not return anything until it's finished, but I would think this would be
very apparant if you just ran the program from command line.

Maybe, if it's not too big, you can post your code again?


Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3637
by: ask | last post by:
Hi NG I'm a bit new to programming c# and have a question regarding ftp by the command prompt. As far as I can see it should be possible to start a process and pipe command streams into it. But...
2
1601
by: palaga | last post by:
Hi I'm starting an external process that writes data to stdout, and I want to get back these data. So I use myProcess.StandardOutput (everything is correctly set, I can read the data). to read...
1
5941
by: solex | last post by:
Hello All, Hopefully someone has run into this error. I have written a class(source below) that launches a thread to monitor the StandardOutput of a System.Diagnostics.Process, in particular I...
0
1476
by: BasicQ | last post by:
I am running an executable from my aspx page with the click of a button. A date is passed as an argument. I am able to get the standardoutput from the Process(Exe) into the label of my page after...
2
8620
by: Al | last post by:
I'm currently attempting to use PLink (the console component of PUTTY - see http://www.chiark.greenend.org.uk/~sgtatham/putty/) as a Telnet component as I may in future need to change to using SSH...
0
2212
by: smimon | last post by:
Hi I'm trying to run a DTS package from a ASP.NET web page using System.Diagnostics.Process. This DTS takes up to 10 minutes to complete, during which, output is generated which i would like to...
1
10302
by: ice9 | last post by:
I seem to be missing something in this straightforward example.. but i have a vbs file that is now simplified to one line: Wscript.Echo "test" When i run the process, i'd like output to be...
5
3411
by: =?Utf-8?B?Z215ZXJz?= | last post by:
Hello, I am attempting to start a cmd.exe process and pass several .vbs scripts (with additional parameters) and then read the output from the scripts and make "notes" in a DataTable (the...
3
1404
by: Clutch152 | last post by:
First off, hello everyone. This site has helped me find answeres to numberous questions. What I want to make is a kill tracker for Wolfenstein enemy territory and I want the program to find kills...
0
7059
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6758
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7010
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5362
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4799
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3003
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1311
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
572
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
203
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.