Hi.

Guys, I need some help. Does anyone know how to Call cross threads in vb.net while accessing the backgroundworker as well? I keep getting error everytime I run my program...

Here is my code:

Imports System.IO.Directory
Imports System.IO
Public Class Video_to_Mp3
    Public folderopen As String
   
    Dim ofd As New OpenFileDialog
    Dim sizetext As String
    Dim length As Double
    Dim proc As New Process 'make it global so dat we can kill it from outside
    Dim avdcfile As String = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) & "\AVDC Folder\convertlog.txt"
    Dim filepath As String = "C:\"
    Dim dirsave As String = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) & "\AVDC Folder\Converted Files\Audio\" & Replace(Format(Now(), "dd/MM/yyyy"), "/", "_") & "\"


   
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBack.Click
        Form1.Show()
        Me.Hide()

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
       dlgOpen.InitialDirectory = filepath
        dlgOpen.Title = "Please Select a File"
        dlgOpen.FileName = " "
        dlgOpen.Multiselect = True
        dlgOpen.ShowDialog()


    End Sub

    Private Sub Video_to_Mp3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        optStandard.Select()

        ListView1.Columns.Clear()
  
        ListView1.Columns.Add("File Name", 550, HorizontalAlignment.Center)
        ListView1.Columns.Add("Output Name", 150, HorizontalAlignment.Left)
        ListView1.Columns.Add("Video Length", 80, HorizontalAlignment.Center)
        ListView1.Columns.Add("Video Size", -2, HorizontalAlignment.Center)
 
        txtOutput.Text = dirsave

    End Sub

    Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
    
    End Sub

    Private Sub dlgOpen_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles dlgOpen.FileOk
        dlgOpen.CheckFileExists = True
        Dim i As Integer = 0
        Dim filename() As String = dlgOpen.FileNames
        Dim FileCount As Integer = dlgOpen.FileNames.Count

 
        While i < dlgOpen.FileNames.Count
         
            Dim str(5) As String
            Dim itm As ListViewItem
            str(0) = dlgOpen.FileNames(i)
            str(1) = Path.GetFileNameWithoutExtension(dlgOpen.FileNames(i))
          
            itm = New ListViewItem(str)
            ListView1.Items.Add(itm)
           
            i = i + 1
        End While
    
    End Sub

    Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click

        For Each lsvrow As ListViewItem In ListView1.SelectedItems
            ListView1.Items.Remove(lsvrow)
        Next

    End Sub

    Private Sub dlgSave_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles dlgSave.FileOk
        Me.txtOutput.Text = dlgSave.FileName
    End Sub

    Private Sub btnConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvert.Click
        BackgroundWorker1.RunWorkerAsync()
        Dim quality As Integer

        Dim i As Integer = 0
        If optHigh.Checked Then
            quality = 20
        ElseIf optStandard.Checked Then
            quality = 10
        Else
            quality = 6
        End If


        While i < ListView1.Items.Count
            ListView1.Items(i).Selected = True
            ListView1.Select()

      
            Dim fO As New FileInfo(dlgOpen.FileName)       
            Dim name = Path.GetFileNameWithoutExtension(ListView1.Items.Item(i).Text)       
            Dim OpenfileName As String
        
            OpenfileName = fO.Name
            CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) & "\AVDC Folder\Converted Files\Audio\" & Replace(Format(Now(), "dd/MM/yyyy"), "/", "_") & "\")
            Dim fbd As New FolderBrowserDialog
            Control.CheckForIllegalCrossThreadCalls = False
            Dim input As String = ListView1.Items.Item(i).Text
            Dim output As String = dirsave & name & ".mp3"
            Dim exepath As String = Application.StartupPath + "\bin\ffmpeg.exe"
           
            Dim startinfo As New System.Diagnostics.ProcessStartInfo
            Dim sr As StreamReader
            Dim cmd As String = " -i """ + input + """ -ar 22050 -qscale " & quality & " -y """ + output + """" 'ffmpeg commands -y replace

            Dim ffmpegOutput As String


            startinfo.FileName = exepath
            startinfo.Arguments = cmd
            startinfo.UseShellExecute = False
            startinfo.WindowStyle = ProcessWindowStyle.Normal
            startinfo.RedirectStandardError = True
            startinfo.RedirectStandardOutput = True
            startinfo.CreateNoWindow = True
            proc.StartInfo = startinfo

            proc.Start()

            'Me.Label2.Text = "Conversion in progress... Please wait..."
            sr = proc.StandardError 'standard error is used by ffmpeg
            Me.btnConvert.Enabled = False
            Me.btnAdd.Enabled = False

            Me.btnRemove.Enabled = False
            Me.btnOutput.Enabled = False

            Do
                If BackgroundWorker1.CancellationPending Then 'check if a cancellation request was made


                    'Exit Function
                End If
                ffmpegOutput = sr.ReadLine
                Me.txtProgress.Text = ffmpegOutput
                ' Label2.Text = ffmpegOutput


            Loop Until proc.HasExited And ffmpegOutput = Nothing Or ffmpegOutput = ""

            Me.txtProgress.Text = "Finished !"

            i = i + 1
        End While

        MsgBox("Finished Converting")
        btnConvert.Enabled = True
        btnAdd.Enabled = True
        btnRemove.Enabled = True
        btnOutput.Enabled = True
    End Sub

    Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        startConversion()
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        If (txtOutput.Text = "") Then
            MsgBox("Please specify file to convert first!")
        Else
            Dim dir As New IO.DirectoryInfo(dirsave)
            If dir.Exists Then
                Dim savedate As String = (dirsave & "\Converted Files\Audio\" & Replace(Format(Now(), "dd/MM/yyyy"), "/", "_"))
                CreateDirectory(savedate)
                dlgSave.InitialDirectory = savedate
            End If

            dlgSave.Title = "Save As"
            dlgSave.ShowDialog()
        End If
    End Sub

    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ListView1.Items.Clear()
    End Sub
End Class

My code converts one file format to another. It works except that while on the process of converting, the whole program seems to pause and I can't access the buttons even the abort..

I would want my project to be able to simultaneously do the converting while enabling other processes to be involved. I've heard it also has something to do with the backgroundworker?


please help.... It would be very much appreciated.

Thank you...

Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        startConversion()
End Sub

Wehere is your "startConversion" function?
I assume the REAL conversation happens on btnConvert_Click, so you have to put all that code in to the "startConversion" function.
Also keep in mind that on updating controls you have to use delegates. You can not acccess them from another thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.