RichTextbox中显示的实时输出

时间:2017-12-18 20:39:52

标签: .net vb.net console runtime real-time

是否有人可以在进程终止之前帮助我从控制台输出中读取。

我有第三方控制台应用程序,他的应用程序可能不会在每行之后刷新数据,而且我无法读取。

还有其他方法,而不是使用像Shell一样的进程或使用任何Windows API在运行时从控制台读取数据吗?

这是我的代码:

Private Sub StartMiner()

        MinerProcess = New Process

        MinerProcess.StartInfo.CreateNoWindow = False
        MinerProcess.StartInfo.FileName = "abc.exe"
        MinerProcess.StartInfo.UseShellExecute = False
        MinerProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal
        MinerProcess.StartInfo.RedirectStandardOutput = True
        MinerProcess.StartInfo.RedirectStandardError = True
        MinerProcess.EnableRaisingEvents = True
        MinerProcess.StartInfo.Arguments = "--server abc"

        MinerProcess.BeginOutputReadLine()
        MinerProcess.BeginErrorReadLine()

        MinerProcess.Start()
        MinerProcess.WaitForExit()

End Sub

Private Sub MinerProcess_OutputDataReceived(sender As Object, e As DataReceivedEventArgs) Handles MinerProcess.OutputDataReceived

        If Me.InvokeRequired Then
            Me.BeginInvoke(CType(Sub()
                                     RichTextBox1.Text = RichTextBox1.Text & e.Data & Environment.NewLine
                                 End Sub, MethodInvoker))
        Else
            RichTextBox1.Text = RichTextBox1.Text & e.Data & Environment.NewLine
        End If

End Sub

此外,我尝试使用读取 BeginRead Readline ReadtoEnd ,但没有任何工作正常。所有方法都有效,但是当我终止进程并且控制台文件是长时间运行的过程时显示输出,因此无法关闭并再次重新打开以获得输出。

1 个答案:

答案 0 :(得分:0)

您可以尝试将fileStream设置为进程stdOutput 例如:

Dim myStreamReader As StreamReader = myProcess.StandardOutput

你可以反复阅读 myStreamReader ......

相关问题