Matthew Bevan

Matthew Bevan

  • NA
  • 1
  • 2.6k

Getting output from Process

Feb 13 2011 5:03 PM
I load a film clip, use FFmpeg to convert it. That all works but I want to show the Process output within a RichTextBox on my Form. I can output the text to a msgBox but don't get any output to the RichTextBox.
I have attached the code used and would be grateful for any help.

Public MediaFolder As String = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) & "\"
    Delegate Sub SetTextCallback(ByVal [text] As String)

    'openfiledialog to load film clip for conversion
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
            If OpenFileDialog1.FileName <> "" Then
                'takes the film, file, and concerts to avi and places in media tmp_videos folder
                Dim ffmpegProg As String = Application.StartupPath & "\ffmpeg.exe"
                Dim FilmSource As String
                Dim FilmDestination As String
                FilmSource = Replace(OpenFileDialog1.FileName, "\", "\\")
                FilmSource = Chr(34) & FilmSource & Chr(34)
                FilmDestination = Chr(34) & MediaFolder & "test.avi" & Chr(34)
                FilmDestination = Replace(FilmDestination, "\", "\\")

                Dim myProcess As System.Diagnostics.Process = New System.Diagnostics.Process()
                myProcess.StartInfo.UseShellExecute = False
                myProcess.StartInfo.CreateNoWindow = True
                myProcess.StartInfo.FileName = ffmpegProg
                myProcess.StartInfo.Arguments = " -i " & FilmSource & " -target pal-dvd -y " & FilmDestination
                myProcess.StartInfo.RedirectStandardOutput = True
                myProcess.StartInfo.RedirectStandardInput = True
                myProcess.StartInfo.RedirectStandardError = True
                myProcess.EnableRaisingEvents = True
                AddHandler myProcess.OutputDataReceived, AddressOf Me.HandleProcessOutput
                AddHandler myProcess.ErrorDataReceived, AddressOf Me.StandardErrorHandler

                ' myProcess.OutputDataReceived += New DataReceivedEvent.Handler(myProcess_OutputDataReceived)
                myProcess.Start()
                myProcess.BeginOutputReadLine()
                myProcess.BeginErrorReadLine()
                    myProcess.WaitForExit()
                    myProcess.Close()
                    RichTextBox1.Text = "finished"
            End If
        End If
    End Sub

    'Output sub
    Private Sub HandleProcessOutput(ByVal sender As Object, ByVal e As System.Diagnostics.DataReceivedEventArgs)
        SetText(e.Data)
    End Sub
    'Error handling
    Private Sub StandardErrorHandler(ByVal sender As Object, ByVal e As System.Diagnostics.DataReceivedEventArgs)
        SetText(e.Data)
    End Sub
    'check which threads 
    Private Sub SetText(ByVal [text] As String)
        If Me.RichTextBox1.InvokeRequired Then
            Dim d As New SetTextCallback(AddressOf SetText)
            Me.Invoke(d, New Object() {[text]})
        Else
            Me.RichTextBox1.Text = [text]
        End If
    End Sub