将进程输出重定向到richtextbox

时间:2013-11-06 20:48:56

标签: vb.net visual-studio-2010 richtextbox appendtext console-redirect

我正在尝试重定向由我的应用程序启动的第三方命令行工具的输出。我正在输出到富文本框。这是成功的,但有一个例外。

当我使用ping命令测试时,我的输出格式不正确。一行附加到前一行。当我尝试在其中放置VbNewLineVbCrLfEnvironment.NewLine时会添加一个空白行。

我得到了这个输出:

Pinging 8.8.8.8 with 32 bytes of data:



Reply from 8.8.8.8: bytes=32 time=29ms TTL=44

Reply from 8.8.8.8: bytes=32 time=29ms TTL=44

Reply from 8.8.8.8: bytes=32 time=30ms TTL=44

Reply from 8.8.8.8: bytes=32 time=29ms TTL=44




Ping statistics for 8.8.8.8:

    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),

Approximate round trip times in milli-seconds:

    Minimum = 29ms, Maximum = 30ms, Average = 29ms

我想得到(就像在CMD窗口中一样):

Pinging 8.8.8.8 with 32 bytes of data:

Reply from 8.8.8.8: bytes=32 time=31ms TTL=44
Reply from 8.8.8.8: bytes=32 time=29ms TTL=44
Reply from 8.8.8.8: bytes=32 time=29ms TTL=44
Reply from 8.8.8.8: bytes=32 time=29ms TTL=44

Ping statistics for 8.8.8.8:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 29ms, Maximum = 31ms, Average = 29ms

我确信这很简单,但无法理解。

这是我正在使用的代码。

Private Sub StartProcess()

    Dim Proc = New Process()

    Proc.StartInfo.FileName = "ping"
    Proc.StartInfo.Arguments = "8.8.8.8"

    Proc.StartInfo.RedirectStandardOutput = True
    Proc.StartInfo.RedirectStandardError = True
    Proc.EnableRaisingEvents = True
    Application.DoEvents()
    Proc.StartInfo.CreateNoWindow = True
    Proc.StartInfo.UseShellExecute = False

    AddHandler Proc.ErrorDataReceived, AddressOf proc_OutputDataReceived
    AddHandler Proc.OutputDataReceived, AddressOf proc_OutputDataReceived
    Proc.Start()
    Proc.BeginErrorReadLine()
    Proc.BeginOutputReadLine()
    'Proc.WaitForExit()

End Sub


Delegate Sub UpdateTextBoxDelg(ByVal text As String)
Public myDelegate As UpdateTextBoxDelg = New UpdateTextBoxDelg(AddressOf UpdateTextBox)

Public Sub UpdateTextBox(ByVal text As String)
    box_output.AppendText(text)
End Sub

Public Sub proc_OutputDataReceived(ByVal sender As Object, ByVal e As DataReceivedEventArgs)
    On Error Resume Next
    If Me.InvokeRequired = True Then
        Me.Invoke(myDelegate, e.Data)
    Else
        UpdateTextBox(e.Data)
    End If

End Sub

2 个答案:

答案 0 :(得分:3)

有点笨蛋,花了我一会儿。我注意到e.Data可以是Nothing,之前从未见过。网络公用事业是特别的,伯克利是地球上不寻常的地方。

你必须自己添加行结尾。使用InvokeRequired是没有意义的,它总是用于OutputDataReceived事件。这个版本运行良好:

Public Sub proc_OutputDataReceived(ByVal sender As Object, ByVal e As DataReceivedEventArgs)
    If e.Data IsNot Nothing Then Me.Invoke(myDelegate, e.Data + vbCrLf)
End Sub

请改用System.Net.NetworkInformation.Ping类。它不受娱乐化学物质的影响。

答案 1 :(得分:0)

它对我来说很好;只需修改您的代码,如下所示:


opt: <stdin>:32:43: error: expected instruction opcode
  store i32 %i.0, i32* %arrayidx, align 4 !0

ScreenShot