【问题标题】:How to interact with process output?如何与流程输出交互?
【发布时间】:2017-09-19 20:42:40
【问题描述】:

好的,现在我有一个程序,它使用 VB.net 中的进程运行 FFmpeg。我在 startinfo 中发送进程参数以及文件位置等其他内容。当我运行代码时,它将控制台输出发送到调试控制台;这可能是因为我有 .UseShellExecute = False 和 processInfo.RedirectStandardOutput = True

我的问题是:如何制作可以解释输出的东西?同样使用 FFmpeg,该过程是连续的,因此该过程大部分时间都在运行,并且在调试控制台中不断添加更多输出行。

我正在使用的代码:

Dim process As New Process
        Dim processInfo As New ProcessStartInfo
        processInfo.FileName = tempPath
        processInfo.Arguments = ("-r 1/.1 -i " + link + " -c copy " + saveLocation + "\" + streamerName + ".ts")
        processInfo.UseShellExecute = False
        processInfo.WindowStyle = ProcessWindowStyle.Hidden
        processInfo.CreateNoWindow = True
        processInfo.RedirectStandardOutput = True
        process.StartInfo = processInfo
        process.Start()

我试过了,没有运气。

Dim output As String
        Using StreamReader As System.IO.StreamReader = process.StandardOutput
            output = StreamReader.ReadToEnd().ToString
        End Using

编辑:我现在有这个代码:

Dim process As New Process
        AddHandler process.OutputDataReceived, AddressOf CallbackProcesoAsync
        AddHandler process.ErrorDataReceived, AddressOf ErrorDataReceivedAsync
        Dim processInfo As New ProcessStartInfo
        processInfo.FileName = tempPath
        processInfo.Arguments = ("-r 1/.1 -i " + link + " -c copy " + saveLocation + "\" + streamerName + ".ts")
        processInfo.UseShellExecute = False
        processInfo.WindowStyle = ProcessWindowStyle.Hidden
        processInfo.CreateNoWindow = False
        processInfo.RedirectStandardOutput = True
        processInfo.RedirectStandardError = True
        process.StartInfo = processInfo
        process.Start()
        processes.Add(Tuple.Create(tempPath, streamerName))
        Debug.WriteLine("Attempting to record " + streamerName)
        Dim output As String
        Using StreamReader As System.IO.StreamReader = process.StandardOutput
            output = StreamReader.ReadToEnd().ToString
        End Using
    End If
End Sub

Private Sub CallbackProcesoAsync(sender As Object, args As System.Diagnostics.DataReceivedEventArgs)
    If Not args.Data Is Nothing AndAlso Not String.IsNullOrEmpty(args.Data) Then
        RichTextBox1.Text = args.Data
    End If
End Sub

Private Sub ErrorDataReceivedAsync(sender As Object, args As System.Diagnostics.DataReceivedEventArgs)
    If Not args.Data Is Nothing AndAlso Not String.IsNullOrEmpty(args.Data) Then
        RichTextBox2.Text = args.Data
    End If
End Sub

但我还没有收到任何到 Richtextbox 的输出?

我觉得它与 streamReader 有关,所以我将其删除,但它仍然无法正常工作?我不知道它会是什么。

【问题讨论】:

  • 您的应用程序不会在output = StreamReader.ReadToEnd().ToString 上崩溃吗?
  • 它不会崩溃,但如果我这样做 RichTextBox1.Text = output 它会崩溃并说就像线程之间的交叉线程 - 因为我对主进程进行了 5 次多线程处理。
  • 虽然我可以调用输出并且一切正常,但我仍然想实时获取正在运行的输出,因为在我结束流后它会显示刚刚发生的输出,所以是我无法实时显示此错误输出吗?
  • 尝试用RichTextBox2.Text &= args.Data & vbnewline 替换RichTextBox2.Text = args.Data 和richtextbox1 一样。
  • 这不起作用,但我设法将 invoke(Sub()) richtextbox1.text = richtextbox1 + output) 这有效,但只有在线程结束后才会显示消息

标签: vb.net shell process ffmpeg output


【解决方案1】:

Ffmpeg 将所有调试信息输出到 StdErr 而不是 StdOut。使用 RedirectStandardError 为 true 并尝试从进程中读取 StandardError。

【讨论】:

  • 有没有办法看到这个更新?我路由标准错误以显示在富文本框中,但我猜文本只会在发生错误时出现?有没有办法显示所有的流程输出文本?
  • 您可以设置回调,以便在有新数据时收到通知。 See this VB example
猜你喜欢
  • 1970-01-01
  • 2010-10-01
  • 2016-03-09
  • 1970-01-01
  • 1970-01-01
  • 2012-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多