tags:

views:

26

answers:

1

I have a .net app that needs to spawn a console process (a java app) and capture the output in order to respond to it in various ways.

I'm not having much trouble getting the process spawned, and getting most output, but some of it is missing. I am assuming it's somehow not going to "standard output", but I need to find out how to capture it.

Public Sub Start()
    Dim cmdArgs As String
    cmdArgs = "-jar """ & Config.ServerJar & """"

    '' Configure the main server process
    mServerProcess = New Process
    With mServerProcess.StartInfo
        .WorkingDirectory = Config.WorkingDirectory
        .FileName = Config.Java
        .Arguments = cmdArgs
        .UseShellExecute = False
        .CreateNoWindow = True
        .RedirectStandardError = True
        .RedirectStandardInput = True
        .RedirectStandardOutput = True
    End With

    '' Wire up an event handler to catch messages out of the process
    AddHandler mServerProcess.OutputDataReceived, AddressOf OutputHandler
    AddHandler mServerProcess.ErrorDataReceived, AddressOf ErrorHandler

    '' Start the server process
    mServerRunning = False
    mServerProcess.Start()

    '' Wire up the writer to send messages to the process
    Dim ioWriter As IO.StreamWriter = mServerProcess.StandardInput
    ioWriter.AutoFlush = True

    '' Start listening for output
    mServerProcess.BeginOutputReadLine()

    '' Sleep for a while to let the server settle
    Threading.Thread.Sleep(5000)

    '' Send test command to the server
    ioWriter.WriteLine("test")

    '' Wait for the server to terminate
    mServerProcess.WaitForExit()
    mServerRunning = False

End Sub

Private Sub OutputHandler(ByVal SendingProcess As Object, _
                          ByVal OutLine As DataReceivedEventArgs)

    If Not OutLine.Data Is Nothing Then
        Debug.Print("STD:" & OutLine.Data)
    End If
End Sub

So, my question is, how can I either all console output?

A: 

I'm an idiot.

I forgot to turn on error output.

    mServerProcess.BeginOutputReadLine()

should be

    mServerProcess.BeginOutputReadLine()
    mServerProcess.BeginErrorReadLine()
Cylindric