How do I use threads or something in that code?

Asked

Viewed 88 times

1

Guys I was needing to use the Threads (or something that doesn’t lock the program while the code doesn’t finish running) in that code, but always gives a different error, so I need your help.

I was starting the action with a button.

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

    Dim var2 As String = "C:\...\Pasta de Video"
    ListView1.Clear()
    PegarImagemDosVideos(var2)
    Esc(var2)

End Sub

That runs this sub that filters some data.

Private Sub Esc(ByVal CaminhoPastaVideos As String)

    Dim gp As New ListViewGroup(CaminhoPastaVideos) With {
            .Name = CaminhoPastaVideos
        }
    ListView1.Groups.Add(gp)

    If FileIO.FileSystem.GetFiles(CaminhoPastaVideos, FileIO.SearchOption.SearchTopLevelOnly, "*.mp4", "*.avi", "*.wmv", "*.mkv").Count > 0 Then             'Pega os videos soltos na pasta.
        For Each video In FileIO.FileSystem.GetFiles(CaminhoPastaVideos, FileIO.SearchOption.SearchTopLevelOnly, "*.mp4", "*.avi", "*.wmv", "*.mkv")
            ColocarPastasDestroDoListView(video, gp)
        Next
    End If
    If FileIO.FileSystem.GetDirectories(CaminhoPastaVideos, FileIO.SearchOption.SearchTopLevelOnly).Count > 0 Then                                           'Verifica se tem pastas na pasta passada na Sub.
        For Each pastas In FileIO.FileSystem.GetDirectories(CaminhoPastaVideos, FileIO.SearchOption.SearchTopLevelOnly)
            If FileIO.FileSystem.GetFiles(pastas, FileIO.SearchOption.SearchTopLevelOnly, "*.mp4", "*.avi", "*.wmv", "*.mkv").Count = 1 Then            'Se tiver menos que 2 videos joga no grupo anterior.
                For Each video In FileIO.FileSystem.GetFiles(pastas, FileIO.SearchOption.SearchAllSubDirectories, "*.mp4", "*.avi", "*.wmv", "*.mkv")
                    ColocarPastasDestroDoListView(video, gp)
                Next
            ElseIf FileIO.FileSystem.GetFiles(pastas, FileIO.SearchOption.SearchAllSubDirectories, "*.mp4", "*.avi", "*.wmv", "*.mkv").Count > 1 Then
                If FileIO.FileSystem.GetFiles(pastas, FileIO.SearchOption.SearchTopLevelOnly, "*.mp4", "*.avi", "*.wmv", "*.mkv").Count >= 1 Then       'Se tiver mais que 1 videos cria um novo grupo e joga os videos nele.
                    Dim gpi As New ListViewGroup(CaminhoPastaVideos) With {
                        .Name = pastas
                    }
                    ListView1.Groups.Add(gpi)
                    For Each video In FileIO.FileSystem.GetFiles(pastas, FileIO.SearchOption.SearchTopLevelOnly, "*.mp4", "*.avi", "*.wmv", "*.mkv")
                        ColocarPastasDestroDoListView(video, gpi)
                    Next
                ElseIf FileIO.FileSystem.GetDirectories(pastas, FileIO.SearchOption.SearchTopLevelOnly).Count > 0 Then                                  'Se tiver menos que 2 videos repete o processo.
                    Esc(pastas)
                End If
            End If
        Next
    End If
End Sub

And finally she executes this code that adds the images to itens of listView.

    Private Sub ColocarPastasDestroDoListView(ByVal Caminho As String, gp As ListViewGroup)

    Dim caminho_saida As String = "C:\Users\...\source\repos\ApenasPronto\ApenasPronto\Thumb\"
    Dim caminho_thumb As String
    Dim name_arquivos As String
    Dim formato_imagem As String = ".png"
    Dim xx As Image

    name_arquivos = System.IO.Path.GetFileNameWithoutExtension(Caminho)
    caminho_thumb = caminho_saida + name_arquivos + formato_imagem

    Using str As Stream = File.OpenRead(caminho_thumb)
        xx = Image.FromStream(str)
        thumbnail.Images.Add(xx)
    End Using

    ListView1.LargeImageList = thumbnail

    Dim lvi As New ListViewItem
    lvi = New ListViewItem With {
        .Text = name_arquivos,
        .ImageIndex = cont,
        .Group = gp
    }
    ListView1.Items.Add(lvi)
    cont += 1
End Sub

Some of the mistakes I had were in the lines: ListView1.Groups.Add(gpi) and ListView1.LargeImageList = thumbnail. Besides I can’t do it Dim td As New Threading.Thread(AddressOf Esc("C:\...\videos"))why the AddressOf does not allow use ("C:\...\videos") then how do I fix it?

Guys, I’ve been racking my brain about this for a long time, so if you can help me figure this out, I’d really appreciate it.

  • Hi @Lucas. You want to use thread where? In the method ColocarPastasDestroDoListView?

  • Yes. This part takes a while to be executed and so the program "freezes" until it finishes.

  • places within a backgroudworcker that the program continues to run normally during work http://www.macoratti.net/vbn5_ept.htm

1 answer

0

Try it this way:

Private Sub ColocarPastasDestroDoListView(ByVal Caminho As String, gp As ListViewGroup)
    Dim caminho_saida As String = "C:\Users\...\source\repos\ApenasPronto\ApenasPronto\Thumb\"
    Dim caminho_thumb As String
    Dim name_arquivos As String
    Dim formato_imagem As String = ".png"
    Dim xx As Image

    Dim thread as New Thread
    (
        Sub()
            name_arquivos = System.IO.Path.GetFileNameWithoutExtension(Caminho)
            caminho_thumb = caminho_saida + name_arquivos + formato_imagem

            Using str As Stream = File.OpenRead(caminho_thumb)
                xx = Image.FromStream(str)

                thumbnail.Invoke(
                    Sub()
                        thumbnail.Images.Add(xx)
                    End Sub
                )
            End Using

            ListView1.Invoke(
                Sub()
                    ListView1.LargeImageList = thumbnail
                End Sub
            )

            Dim lvi As New ListViewItem

            lvi = New ListViewItem With 
            {
                .Text = name_arquivos,
                .ImageIndex = cont,
                .Group = gp
            }

            ListView1.Invoke(
                Sub()
                    ListView1.Items.Add(lvi)
                End Sub
            )

            cont += 1
        End Sub
    )

    thread.Start()

End Sub

The syntax and details of the language fail me a little, so you may have to adjust some points.

Another solution would be to use a BackgroundWorker and the events DoWork, ProgressChanged and RunWorkerCompleted, if you want to have more control over what is being done during the processing of Thread.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.