10-31-2017, 07:25 AM
Hello Everyone, Admin. I'm new to this Forum and i want to ask how to search files from a certain directory with progressbar & background worker?Â
Code: (from the Admin-(Brandonio)) :
----------------------------------------------------------------------------------------------------------
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    BackgroundWorker1.RunWorkerAsync()
  End Sub
  Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    Dim directory = "Path"
    Dim files() As System.IO.FileInfo
    Dim dirinfo As New System.IO.DirectoryInfo(directory)
    files = dirinfo.GetFiles("*", IO.SearchOption.AllDirectories)
    For Each file In files
      ListBox1.Items.Add(file)
    Next
  End Sub
----------------------------------------------------------------------------------------------------------
But whenever i click the button, i get an error saying :Â Cross-thread operation not valid: Control 'ListBox1' accessed from a thread other than the thread it was created on.
----------------------------------------------------------------------------------------------------------
I'm new in VB.NET and i'm making an application that cleans up the Temporary folder.
Thank you. and uh, i like this forum.
Code: (from the Admin-(Brandonio)) :
----------------------------------------------------------------------------------------------------------
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    BackgroundWorker1.RunWorkerAsync()
  End Sub
  Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    Dim directory = "Path"
    Dim files() As System.IO.FileInfo
    Dim dirinfo As New System.IO.DirectoryInfo(directory)
    files = dirinfo.GetFiles("*", IO.SearchOption.AllDirectories)
    For Each file In files
      ListBox1.Items.Add(file)
    Next
  End Sub
----------------------------------------------------------------------------------------------------------
But whenever i click the button, i get an error saying :Â Cross-thread operation not valid: Control 'ListBox1' accessed from a thread other than the thread it was created on.
----------------------------------------------------------------------------------------------------------
I'm new in VB.NET and i'm making an application that cleans up the Temporary folder.
Thank you. and uh, i like this forum.