473,417 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Redirecting StandardOutput in realtime

Hi

I created a windows app to run a dos batch file (which takes around
5mins to complete and generates lots of output messages on the console
in the meantime)and i used RedirectStandardOuput to display the output
text in a textbox.

The problem is that till the batch file has not finished running i
don't get to see the output in the text box!!

How do we trap the output and display as and when its generated by the
called app? can we get a LIVE UPDATE of the output?

Attached is part of the code for completeness :-

Your help will be greatly appreciated.

Thanks

Vijay

--------- CODE FOLLOWS -------------

// Create Process and Execute
p = new Process();

// Set Start Info
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow=false;
p.StartInfo.RedirectStandardOutput = false;
p.StartInfo.FileName = cmd;
p.StartInfo.WorkingDirectory="C:\\xyz\\";
p.Start();

StreamReader myStreamReader = p.StandardOutput;
// Read the standard output of the spawned process.
while((output = myStreamReader.ReadLine())!=null)
{
this.txtOutput.Text += output + "\r\n";
}
p.Close();

Nov 17 '05 #1
5 12745
hi,

I would do several changes:

First of all I would run this in a worker thread ( I will assume this)

p.StartInfo.RedirectStandardOutput = true;

while((output = myStreamReader.ReadLine())!=null)
{
string[] args = new string[1];
string[0] = output;
this.txtOutput.Invoke( UpdateOutput, args );
}

void UpdateTXTBOX( string line)
{
this.txtbox.Text+=line;
}

these are the declarations

public delegate void EventString( string line);

public EventString UpdateOutput = new EventString ( UpdateTXTBOX);

With that it should work.
Also consider change from a Textbox to something else, the strings
operations may have a performance impact.
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
<vi*******@yahoo.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
Hi

I created a windows app to run a dos batch file (which takes around
5mins to complete and generates lots of output messages on the console
in the meantime)and i used RedirectStandardOuput to display the output
text in a textbox.

The problem is that till the batch file has not finished running i
don't get to see the output in the text box!!

How do we trap the output and display as and when its generated by the
called app? can we get a LIVE UPDATE of the output?

Attached is part of the code for completeness :-

Your help will be greatly appreciated.

Thanks

Vijay

--------- CODE FOLLOWS -------------

// Create Process and Execute
p = new Process();

// Set Start Info
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow=false;
p.StartInfo.RedirectStandardOutput = false;
p.StartInfo.FileName = cmd;
p.StartInfo.WorkingDirectory="C:\\xyz\\";
p.Start();

StreamReader myStreamReader = p.StandardOutput;
// Read the standard output of the spawned process.
while((output = myStreamReader.ReadLine())!=null)
{
this.txtOutput.Text += output + "\r\n";
}
p.Close();

Nov 17 '05 #2
Hi

I tried your code...created a thread to spawn the process - still the
same - The standard output of the spawned process is available only
after the process has finished execution.

Since the called batch file is run in a different process, i guess we
need some kind of Interprocess communication (IPC) to send the output
of the spawned process to the calling process (my C# windows app).

Something like this -
----------------------

My App Spawned Process (batch file)
------- ----------------------------
Create Process ----------------------> Process Created
.. some processing...
.. some processing...
.. some output...
Receive Notification <------------------Send output text to 'My App'
Display in window ..some processing..
.. some processing...
.. some output...
Receive Notification <------------------Send output text to 'My App'
Display in window ..some processing..
.. some processing...
.. some output...
Receive Notification <------------------Send output text to 'My App'
Display in window ..some processing..
..Process Exit
Display Completed

But right now its happenning like this -
----------------------------------------
My App Spawned Process (batch file)
------- ----------------------------
Create Process ----------------------> Process Created
.. some processing...
.. some processing...
.. some output...(buffered ??)
..some processing..
.. some processing...
.. some output...(buffered ??)
.. some processing...
.. some output...(buffered ??)
..some processing..
..Process Exit
Read the output<--------------------- Send output text to 'My App'
Display the output
Display Completed

It would be great if yuu could throw some light on the internal working
of Process.start and how the output is made available to the calling
process and in what sequence.

Thanks

Vijay

Ignacio Machin ( .NET/ C# MVP ) wrote:
hi,

I would do several changes:

First of all I would run this in a worker thread ( I will assume this)

p.StartInfo.RedirectStandardOutput = true;

while((output = myStreamReader.ReadLine())!=null)
{
string[] args = new string[1];
string[0] = output;
this.txtOutput.Invoke( UpdateOutput, args );
}

void UpdateTXTBOX( string line)
{
this.txtbox.Text+=line;
}

these are the declarations

public delegate void EventString( string line);

public EventString UpdateOutput = new EventString ( UpdateTXTBOX);

With that it should work.
Also consider change from a Textbox to something else, the strings
operations may have a performance impact.
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
<vi*******@yahoo.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
Hi

I created a windows app to run a dos batch file (which takes around
5mins to complete and generates lots of output messages on the console
in the meantime)and i used RedirectStandardOuput to display the output
text in a textbox.

The problem is that till the batch file has not finished running i
don't get to see the output in the text box!!

How do we trap the output and display as and when its generated by the
called app? can we get a LIVE UPDATE of the output?

Attached is part of the code for completeness :-

Your help will be greatly appreciated.

Thanks

Vijay

--------- CODE FOLLOWS -------------

// Create Process and Execute
p = new Process();

// Set Start Info
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow=false;
p.StartInfo.RedirectStandardOutput = false;
p.StartInfo.FileName = cmd;
p.StartInfo.WorkingDirectory="C:\\xyz\\";
p.Start();

StreamReader myStreamReader = p.StandardOutput;
// Read the standard output of the spawned process.
while((output = myStreamReader.ReadLine())!=null)
{
this.txtOutput.Text += output + "\r\n";
}
p.Close();


Nov 17 '05 #3
hi,

no really, the ipc mechanism is the stream. it's created for you by the OS.

did you google for it?

I did and found these:
http://groups-beta.google.com/group/...e3ca48761b3195

http://groups-beta.google.com/group/...cce4bc96070bb5

It seems that one possible problem is that the output may be buffered

maybe changing the size of the buffer, I have no clear idea of how to do
this though :(

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
<vi*******@yahoo.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
Hi

I tried your code...created a thread to spawn the process - still the
same - The standard output of the spawned process is available only
after the process has finished execution.

Since the called batch file is run in a different process, i guess we
need some kind of Interprocess communication (IPC) to send the output
of the spawned process to the calling process (my C# windows app).

Something like this -
----------------------

My App Spawned Process (batch file)
------- ----------------------------
Create Process ----------------------> Process Created
.. some processing...
.. some processing...
.. some output...
Receive Notification <------------------Send output text to 'My App'
Display in window ..some processing..
.. some processing...
.. some output...
Receive Notification <------------------Send output text to 'My App'
Display in window ..some processing..
.. some processing...
.. some output...
Receive Notification <------------------Send output text to 'My App'
Display in window ..some processing..
..Process Exit
Display Completed

But right now its happenning like this -
----------------------------------------
My App Spawned Process (batch file)
------- ----------------------------
Create Process ----------------------> Process Created
.. some processing...
.. some processing...
.. some output...(buffered ??)
..some processing..
.. some processing...
.. some output...(buffered ??)
.. some processing...
.. some output...(buffered ??)
..some processing..
..Process Exit
Read the output<--------------------- Send output text to 'My App'
Display the output
Display Completed

It would be great if yuu could throw some light on the internal working
of Process.start and how the output is made available to the calling
process and in what sequence.

Thanks

Vijay

Ignacio Machin ( .NET/ C# MVP ) wrote:
hi,

I would do several changes:

First of all I would run this in a worker thread ( I will assume this)

p.StartInfo.RedirectStandardOutput = true;

while((output = myStreamReader.ReadLine())!=null)
{
string[] args = new string[1];
string[0] = output;
this.txtOutput.Invoke( UpdateOutput, args );
}

void UpdateTXTBOX( string line)
{
this.txtbox.Text+=line;
}

these are the declarations

public delegate void EventString( string line);

public EventString UpdateOutput = new EventString ( UpdateTXTBOX);

With that it should work.
Also consider change from a Textbox to something else, the strings
operations may have a performance impact.
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
<vi*******@yahoo.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
> Hi
>
> I created a windows app to run a dos batch file (which takes around
> 5mins to complete and generates lots of output messages on the console
> in the meantime)and i used RedirectStandardOuput to display the output
> text in a textbox.
>
> The problem is that till the batch file has not finished running i
> don't get to see the output in the text box!!
>
> How do we trap the output and display as and when its generated by the
> called app? can we get a LIVE UPDATE of the output?
>
> Attached is part of the code for completeness :-
>
> Your help will be greatly appreciated.
>
> Thanks
>
> Vijay
>
> --------- CODE FOLLOWS -------------
>
> // Create Process and Execute
> p = new Process();
>
> // Set Start Info
> p.StartInfo.UseShellExecute = false;
> p.StartInfo.CreateNoWindow=false;
> p.StartInfo.RedirectStandardOutput = false;
> p.StartInfo.FileName = cmd;
> p.StartInfo.WorkingDirectory="C:\\xyz\\";
> p.Start();
>
> StreamReader myStreamReader = p.StandardOutput;
> // Read the standard output of the spawned process.
> while((output = myStreamReader.ReadLine())!=null)
> {
> this.txtOutput.Text += output + "\r\n";
> }
> p.Close();
>

Nov 17 '05 #4
The problem was becoz the launched process was blocking the UI thread.
Finally i found a way using which it works -

As you mentioned earlier..we have to use one thread each to create the
process, read the stdout and the stderr. These links may prove useful
to those following this topic -]

http://www.codeproject.com/csharp/LaunchProcess.asp

http://msdn.microsoft.com/msdnmag/is...g/default.aspx

Thanks a ton Ignacio!

Cheers!

Vijay

Ignacio Machin ( .NET/ C# MVP ) wrote:
hi,

no really, the ipc mechanism is the stream. it's created for you by the OS.

did you google for it?

I did and found these:
http://groups-beta.google.com/group/...e3ca48761b3195

http://groups-beta.google.com/group/...cce4bc96070bb5

It seems that one possible problem is that the output may be buffered

maybe changing the size of the buffer, I have no clear idea of how to do
this though :(

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
<vi*******@yahoo.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
Hi

I tried your code...created a thread to spawn the process - still the
same - The standard output of the spawned process is available only
after the process has finished execution.

Since the called batch file is run in a different process, i guess we
need some kind of Interprocess communication (IPC) to send the output
of the spawned process to the calling process (my C# windows app).

Something like this -
----------------------

My App Spawned Process (batch file)
------- ----------------------------
Create Process ----------------------> Process Created
.. some processing...
.. some processing...
.. some output...
Receive Notification <------------------Send output text to 'My App'
Display in window ..some processing..
.. some processing...
.. some output...
Receive Notification <------------------Send output text to 'My App'
Display in window ..some processing..
.. some processing...
.. some output...
Receive Notification <------------------Send output text to 'My App'
Display in window ..some processing..
..Process Exit
Display Completed

But right now its happenning like this -
----------------------------------------
My App Spawned Process (batch file)
------- ----------------------------
Create Process ----------------------> Process Created
.. some processing...
.. some processing...
.. some output...(buffered ??)
..some processing..
.. some processing...
.. some output...(buffered ??)
.. some processing...
.. some output...(buffered ??)
..some processing..
..Process Exit
Read the output<--------------------- Send output text to 'My App'
Display the output
Display Completed

It would be great if yuu could throw some light on the internal working
of Process.start and how the output is made available to the calling
process and in what sequence.

Thanks

Vijay

Ignacio Machin ( .NET/ C# MVP ) wrote:
hi,

I would do several changes:

First of all I would run this in a worker thread ( I will assume this)

p.StartInfo.RedirectStandardOutput = true;

while((output = myStreamReader.ReadLine())!=null)
{
string[] args = new string[1];
string[0] = output;
this.txtOutput.Invoke( UpdateOutput, args );
}

void UpdateTXTBOX( string line)
{
this.txtbox.Text+=line;
}

these are the declarations

public delegate void EventString( string line);

public EventString UpdateOutput = new EventString ( UpdateTXTBOX);

With that it should work.
Also consider change from a Textbox to something else, the strings
operations may have a performance impact.
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
<vi*******@yahoo.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
> Hi
>
> I created a windows app to run a dos batch file (which takes around
> 5mins to complete and generates lots of output messages on the console
> in the meantime)and i used RedirectStandardOuput to display the output
> text in a textbox.
>
> The problem is that till the batch file has not finished running i
> don't get to see the output in the text box!!
>
> How do we trap the output and display as and when its generated by the
> called app? can we get a LIVE UPDATE of the output?
>
> Attached is part of the code for completeness :-
>
> Your help will be greatly appreciated.
>
> Thanks
>
> Vijay
>
> --------- CODE FOLLOWS -------------
>
> // Create Process and Execute
> p = new Process();
>
> // Set Start Info
> p.StartInfo.UseShellExecute = false;
> p.StartInfo.CreateNoWindow=false;
> p.StartInfo.RedirectStandardOutput = false;
> p.StartInfo.FileName = cmd;
> p.StartInfo.WorkingDirectory="C:\\xyz\\";
> p.Start();
>
> StreamReader myStreamReader = p.StandardOutput;
> // Read the standard output of the spawned process.
> while((output = myStreamReader.ReadLine())!=null)
> {
> this.txtOutput.Text += output + "\r\n";
> }
> p.Close();
>


Nov 17 '05 #5
hi,

Good to ear you solve your problem

Why don't you post some code, it may help another person and will be stored
in the archives :)

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
<vi*******@yahoo.com> wrote in message
news:11********************@g14g2000cwa.googlegrou ps.com...
The problem was becoz the launched process was blocking the UI thread.
Finally i found a way using which it works -

As you mentioned earlier..we have to use one thread each to create the
process, read the stdout and the stderr. These links may prove useful
to those following this topic -]

http://www.codeproject.com/csharp/LaunchProcess.asp

http://msdn.microsoft.com/msdnmag/is...g/default.aspx

Thanks a ton Ignacio!

Cheers!

Vijay

Ignacio Machin ( .NET/ C# MVP ) wrote:
hi,

no really, the ipc mechanism is the stream. it's created for you by the
OS.

did you google for it?

I did and found these:
http://groups-beta.google.com/group/...e3ca48761b3195

http://groups-beta.google.com/group/...cce4bc96070bb5

It seems that one possible problem is that the output may be buffered

maybe changing the size of the buffer, I have no clear idea of how to do
this though :(

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
<vi*******@yahoo.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
> Hi
>
> I tried your code...created a thread to spawn the process - still the
> same - The standard output of the spawned process is available only
> after the process has finished execution.
>
> Since the called batch file is run in a different process, i guess we
> need some kind of Interprocess communication (IPC) to send the output
> of the spawned process to the calling process (my C# windows app).
>
> Something like this -
> ----------------------
>
> My App Spawned Process (batch file)
> ------- ----------------------------
> Create Process ----------------------> Process Created
> .. some processing...
> .. some processing...
> .. some output...
> Receive Notification <------------------Send output text to 'My App'
> Display in window ..some processing..
> .. some processing...
> .. some output...
> Receive Notification <------------------Send output text to 'My App'
> Display in window ..some processing..
> .. some processing...
> .. some output...
> Receive Notification <------------------Send output text to 'My App'
> Display in window ..some processing..
> ..Process Exit
> Display Completed
>
> But right now its happenning like this -
> ----------------------------------------
> My App Spawned Process (batch file)
> ------- ----------------------------
> Create Process ----------------------> Process Created
> .. some processing...
> .. some processing...
> .. some output...(buffered ??)
> ..some processing..
> .. some processing...
> .. some output...(buffered ??)
> .. some processing...
> .. some output...(buffered ??)
> ..some processing..
> ..Process Exit
> Read the output<--------------------- Send output text to 'My App'
> Display the output
> Display Completed
>
> It would be great if yuu could throw some light on the internal working
> of Process.start and how the output is made available to the calling
> process and in what sequence.
>
> Thanks
>
> Vijay
>
>
>
> Ignacio Machin ( .NET/ C# MVP ) wrote:
>> hi,
>>
>> I would do several changes:
>>
>> First of all I would run this in a worker thread ( I will assume this)
>>
>>
>>
>> p.StartInfo.RedirectStandardOutput = true;
>>
>> while((output = myStreamReader.ReadLine())!=null)
>> {
>> string[] args = new string[1];
>> string[0] = output;
>> this.txtOutput.Invoke( UpdateOutput, args );
>> }
>>
>> void UpdateTXTBOX( string line)
>> {
>> this.txtbox.Text+=line;
>> }
>>
>> these are the declarations
>>
>> public delegate void EventString( string line);
>>
>> public EventString UpdateOutput = new EventString ( UpdateTXTBOX);
>>
>>
>>
>> With that it should work.
>> Also consider change from a Textbox to something else, the strings
>> operations may have a performance impact.
>>
>>
>> cheers,
>>
>> --
>> Ignacio Machin,
>> ignacio.machin AT dot.state.fl.us
>> Florida Department Of Transportation
>>
>>
>> <vi*******@yahoo.com> wrote in message
>> news:11**********************@g43g2000cwa.googlegr oups.com...
>> > Hi
>> >
>> > I created a windows app to run a dos batch file (which takes around
>> > 5mins to complete and generates lots of output messages on the
>> > console
>> > in the meantime)and i used RedirectStandardOuput to display the
>> > output
>> > text in a textbox.
>> >
>> > The problem is that till the batch file has not finished running i
>> > don't get to see the output in the text box!!
>> >
>> > How do we trap the output and display as and when its generated by
>> > the
>> > called app? can we get a LIVE UPDATE of the output?
>> >
>> > Attached is part of the code for completeness :-
>> >
>> > Your help will be greatly appreciated.
>> >
>> > Thanks
>> >
>> > Vijay
>> >
>> > --------- CODE FOLLOWS -------------
>> >
>> > // Create Process and Execute
>> > p = new Process();
>> >
>> > // Set Start Info
>> > p.StartInfo.UseShellExecute = false;
>> > p.StartInfo.CreateNoWindow=false;
>> > p.StartInfo.RedirectStandardOutput = false;
>> > p.StartInfo.FileName = cmd;
>> > p.StartInfo.WorkingDirectory="C:\\xyz\\";
>> > p.Start();
>> >
>> > StreamReader myStreamReader = p.StandardOutput;
>> > // Read the standard output of the spawned process.
>> > while((output = myStreamReader.ReadLine())!=null)
>> > {
>> > this.txtOutput.Text += output + "\r\n";
>> > }
>> > p.Close();
>> >
>

Nov 17 '05 #6

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

Similar topics

3
by: Al Cohen | last post by:
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...
15
by: Matt Burland | last post by:
I'm having a problem with redirecting the StandardOutput of a process that I use to run a DOS program in my application. The problem is that I when I start my process (which I do in a separate...
5
by: Tim | last post by:
Hi I am running a console program through my own VB front end. I am redirecting the StandardOut and StandardInput streams to and from textboxes so that the VB front end acts like a console. My...
6
by: Christophe Helfer | last post by:
hi, I have some problem with redirecting input and output from a process. Situation: I have to use the Cisco Network Registrar (DNS And DHCP server) command line utility as redirecting its...
5
by: Markus S. | last post by:
Hello, I have a problem with a DOS EXE that is called by a .Net Winforms application. I need to redirect the console output into a textbox, but this should happen in real time, so when new...
1
by: rubikzube* | last post by:
I am attempting to place a call to make via System.Diagnostics.Process using the sample code below. If I comment out the two problem lines indicated, then the code runs smoothly and make performs...
3
by: Sudesh | last post by:
Hi, I am a newbie to C# and Im trying to redirect standard input, output and error of a console program written in C (MS VC 6.0) to a textbox on a form. The code for the redirecting looks like...
3
by: mhmtzdmr | last post by:
Hi, I want to run an application and capture its standard output. But the following code does not generate any output. Can anyone see something wrong? Public Sub RunApp(ByVal myprocess As...
1
by: lapucca | last post by:
Hi, I'm using VS 2005, ,.net 2 for C# windows application. I'm using Process to run a C application and redirecting its standard output so I can read it with StreamReader.ReadToEnd. It's only...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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
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,...
0
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.