【问题标题】:Real time output showing in RichTextbox实时输出显示在 RichTextbox
【发布时间】:2017-12-18 20:39:52
【问题描述】:

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

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

除了使用 Shell 之类的 Process 或使用任何 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

我也尝试过使用 ReadBeginReadReadlineReadtoEnd ,但没有我需要的工作。所有方法都有效,但在我终止进程时显示输出,并且控制台文件是长时间运行的进程,因此无法关闭并再次重新打开以获取输出。

【问题讨论】:

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


    【解决方案1】:

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

    Dim myStreamReader As StreamReader = myProcess.StandardOutput
    

    你可以反复阅读myStreamReader...

    【讨论】:

    • 我确实尝试过 mate 但它在控制台终止后显示输出
    • @SunnyGRT 这样你就可以检查进程是否仍然存在,对吧?
    • 不,我无法实时查看进度。当进程终止时,它会显示输出。
    猜你喜欢
    • 2011-12-17
    • 2011-08-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-12
    • 2015-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多