从进程中捕获所有控制台

时间:2010-10-14 19:08:59

标签: .net console

我有一个.net应用程序需要生成一个控制台进程(一个java应用程序)并捕获输出以便以各种方式响应它。

我没有太多麻烦让这个过程产生,并获得大部分输出,但其中一些缺失。我假设它不知何故不会“标准输出”,但我需要找出如何捕获它。

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

所以,我的问题是,我怎样才能所有控制台输出?

1 个答案:

答案 0 :(得分:0)

我是个白痴。

我忘了打开错误输出。

    mServerProcess.BeginOutputReadLine()

应该是

    mServerProcess.BeginOutputReadLine()
    mServerProcess.BeginErrorReadLine()