监视不一定使用 CR/LF 的过程标准输出

发布于 2024-07-25 06:59:45 字数 241 浏览 7 评论 0原文

我的应用程序定期使用 process.start 启动控制台程序。 我需要“实时”监控程序的输出。

例如,程序将以下文本写入控制台: 处理中......

每隔一秒左右就会出现一个新点,让用户知道程序仍在处理中。 但是,...直到程序输出 CR/LF 之前,我无法检索程序的标准输出(当它仍在运行时)。

我该怎么做才能实时获取输出 - 比方说 - 将其通过管道传输到数据库(例如 VB.NET 中)?

My application periodically starts console programs with process.start. I need to monitor the output of the programs in "realtime".

For example, the program writes the following text to the console:
Processing.................

Every second or so a new dot appears to let the user know the program is still processing. However,... until the programm outputs a CR/LF, I am not able to retrieve the standard output of the program (while it is still running).

What can I do to get the output in realtime for - let's say - piping it into a database for instance in VB.NET?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ㄖ落Θ余辉 2024-08-01 06:59:45

将输出发送到文本文件并每秒读取该文件怎么样?

what about sending output into a text file and reading that file every second?

忘羡 2024-08-01 06:59:45

我很沮丧,因为我家里确实有一个原型应用程序做了类似的事情。 我看看能不能拿到。 同时查看此链接:

http: //msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx

它显示了如何将应用程序的输出重定向到自定义流,例如

Public Class Form1

    Private _p As Process

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim psi As New ProcessStartInfo()

        psi.FileName = "C:\mydir.bat"
        psi.RedirectStandardOutput = True
        psi.UseShellExecute = False

        _p = New Process()
        _p.Start(psi)
        tmrReadConsole.Enabled = True
    End Sub

    Private Sub tmrReadConsole_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrReadConsole.Tick
        If _p IsNot Nothing Then
            txtConsoleOutput.Text = _p.StandardOutput.ReadToEnd()
        End If

    End Sub
End Class

上面是一个具有计时器的网络表单它用于轮询控制台的输出流并将其内容放入文本框中。 它不太有效(因此我想找到我的其他应用程序),但你明白了。

希望这可以帮助。

I'm gutted since I did have a prototype application at home that did something like this. I'll see if I can fetch it. In the meantime have a look at this link:

http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx

It shows how to redirect the output of applications to a custom stream e.g.

Public Class Form1

    Private _p As Process

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim psi As New ProcessStartInfo()

        psi.FileName = "C:\mydir.bat"
        psi.RedirectStandardOutput = True
        psi.UseShellExecute = False

        _p = New Process()
        _p.Start(psi)
        tmrReadConsole.Enabled = True
    End Sub

    Private Sub tmrReadConsole_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrReadConsole.Tick
        If _p IsNot Nothing Then
            txtConsoleOutput.Text = _p.StandardOutput.ReadToEnd()
        End If

    End Sub
End Class

The above is a webform that has a timer which is used to poll the output stream of a console and get it's content into a textbox. It doesn't quite work (hence why I want to find my other app), but you get the idea.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文