BP Forums
How to: Search files from a certain directory with Progressbar + Backgroundworker - Printable Version

+- BP Forums (https://bpforums.info)
+-- Forum: Programming Discussion (https://bpforums.info/forumdisplay.php?fid=34)
+--- Forum: VB.NET (https://bpforums.info/forumdisplay.php?fid=43)
+--- Thread: How to: Search files from a certain directory with Progressbar + Backgroundworker (/showthread.php?tid=920)



How to: Search files from a certain directory with Progressbar + Backgroundworker - ryanshane91 - 10-31-2017

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.


RE: How to: Search files from a certain directory with Progressbar + Backgroundworker - brandonio21 - 11-25-2017

Hello ryanshane91 and welcome to BP Forums!

In most programming environments, VB.NET included, there is a main user-interface thread which is the only thread that is allowed to access user interface components. In VB.NET, a BackgroundWorker works on a thread separate than the user-interface thread and thus is not allowed to access user interface components.

There are thus two ways to solve your problem:
1. Tell VB.NET to ignore this restriction and allow non user-interface threads to access the user-interface (
Code:
System.Windows.Forms.Form.CheckForIllegalCrossThreadCalls = false;
)

2. The `BackgroundWorker` has an event to report progress. You can then handle this report on the UI thread. You can do this by:
2a. Setting BackgroundWorker1.WorkerReportsProgress = True
2b. In `BackgroundWorker1_DoWork`, do something like `BackgroundWorker1.ReportProgress(file)`
2c. A new sub which handles the reported progress to add the file to the listbox.

I hope this helps!