How do I use threads or something similar in this code?

1

I was going to need to use the Threads (or something that does not crash the program while the code does not finish) in this code, but it 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 executes 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 it runs this code that adds the images in 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 errors I had were in the lines: ListView1.Groups.Add(gpi) and ListView1.LargeImageList = thumbnail . Besides not being able to do this Dim td As New Threading.Thread(AddressOf Esc("C:\...\videos")) because AddressOf does not allow ("C:\...\videos") so how do I solve this?

I've been doing this for a while, so if you can help me solve this, thank you very much.

    
asked by anonymous 14.09.2018 / 03:19

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 BackgroundWorker and events DoWork , ProgressChanged and RunWorkerCompleted , if you want to have more control over what is being done while processing Thread . >     

18.09.2018 / 14:04