Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / October 2007

Tip: Looking for answers? Try searching our database.

Redirect lengthy application's output?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Sin Jeong-hun - 21 Oct 2007 17:35 GMT
Process p = new Process();
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.Start();
p.StandardOutput.ReadToEnd();

This works if the application outputs very little amount of string and
exits quickly. But what if the application does a lengthy job and
outputs the progress and my C# application should be notified if the
job is progressed? Should I periodically invoke ReadToEnd() in a while
loop? Seems a waste of cpu cycles. Can't I just get notified when the
application outputs something instead? I tried to search the Internet
myself, but I couldn't find an answer.
Andrew Faust - 21 Oct 2007 23:55 GMT
Sure can. The Process class has an event called OutputDataReceived that is
reaised when something is written to the redirected StandardOutput stream.

http://msdn2.microsoft.com/en-us/library/system.diagnostics.process_members.aspx

Signature

Andrew Faust
andrew[at]andrewfaust.com
http://www.andrewfaust.com

> Process p = new Process();
> p.StartInfo.RedirectStandardOutput = true;
[quoted text clipped - 9 lines]
> application outputs something instead? I tried to search the Internet
> myself, but I couldn't find an answer.
Peter Duniho - 22 Oct 2007 00:22 GMT
> [...]
> p.StandardOutput.ReadToEnd();
[quoted text clipped - 3 lines]
> outputs the progress and my C# application should be notified if the
> job is progressed? [...]

As an alternative to using the OutputDataReceived event mentioned in the
other reply, you may also just use the async methods on the
StandardOutput stream.  They will work just as the async methods for
Stream do with other streams, and provide nice asynchronous access to
the data being written to the stream.

You definitely would not want to call ReadToEnd() in a while loop.  It
won't complete until the stream has actually ended (is closed), which
occurs when the process exits.

Pete

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.