views:

359

answers:

2

My application periodically starts console programs with process.start. I need to monitor the output of the programs in "realtime".

For example, the program writes the following text to the console: Processing.................

Every second or so a new dot appears to let the user know the program is still processing. However,... until the programm outputs a CR/LF, I am not able to retrieve the standard output of the program (while it is still running).

What can I do to get the output in realtime for - let's say - piping it into a database for instance in VB.NET?

+1  A: 

what about sending output into a text file and reading that file every second?

dusoft
Unfortunately I don't control the console applications. Also, unless the console app sends a carriage return/line feed (CRLF), I don't evenget any data (or am I doing anything wrong?)
Mephisztoe
@Tronex - if you can get the standard output, you can redirect it to a file. You don't actually have to change the program itself, just use the ">" operator when the program is started to redirect the output to a file.
Eric Petroelje
+1  A: 

Hi there.

I'm gutted since I did have a prototype application at home that did something like this. I'll see if I can fetch it. In the meantime have a look at this link:

http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx

It shows how to redirect the output of applications to a custom stream e.g.

Public Class Form1

    Private _p As Process

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim psi As New ProcessStartInfo()

        psi.FileName = "C:\mydir.bat"
        psi.RedirectStandardOutput = True
        psi.UseShellExecute = False

        _p = New Process()
        _p.Start(psi)
        tmrReadConsole.Enabled = True
    End Sub

    Private Sub tmrReadConsole_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrReadConsole.Tick
        If _p IsNot Nothing Then
            txtConsoleOutput.Text = _p.StandardOutput.ReadToEnd()
        End If

    End Sub
End Class

The above is a webform that has a timer which is used to poll the output stream of a console and get it's content into a textbox. It doesn't quite work (hence why I want to find my other app), but you get the idea.

Hope this helps. Jas.

Jason Evans
Try this for the button click:Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click _p = New Process _p.StartInfo.FileName = "C:\mydir.bat" _p.StartInfo.RedirectStandardOutput = True _p.StartInfo.UseShellExecute = False _p.Start() txtConsoleOutput.Text = _p.StandardOutput.ReadToEnd End Sub
Jason Evans