Recursive File/Folder search on different thread

Posted on 2012-09-03
Medium Priority
Last Modified: 2012-10-01

I am trying to use this code in a multi-threaded WPF Visual Basic .NET application:

Private Sub RecursiveSearch(ByVal strPath As String, ByVal strPattern As String, ByRef lstTarget As ListBox)

        Dim strFolders() As String = System.IO.Directory.GetDirectories(strPath)
        Dim strFiles() As String = System.IO.Directory.GetFiles(strPath, strPattern)
        Dim clsFile As FileInfoEx = Nothing

        'Add the files
        For Each strFile As String In strFiles
            clsFile = New FileInfoEx(strFile)

        'Look through the other folders
        For Each strFolder As String In strFolders
            'Call the procedure again to perform the same operation
            RecursiveSearch(strFolder, strPattern, lstTarget)

    End Sub

(Sourced from here http://vbcity.com/forums/t/136964.aspx)

This code works really well for recursively finding files that match a specific pattern by walking down a directory structure of folders and sub-folders.

The problem comes in with the fact that I am trying to run it on a separate thread and update a listbox with entries *as the files are found*.  Becuse the folders I will be running this on can contain tens of thousands of files, the application seems to be unresponsive while the search is running and as such, I want to update the UI as each matching file is found and added to te list.

I have tried using backgroundworkers, delegates and threads but I cannot get it to work - I get all manner of errors from claims that the backgroundworker does not report progress (even though the property for it is set to do so) and on other occasions nothing happens, thread-lcock errors occur, etc.

Please help - just a simple (tested) example of running this type of file search on a thread that updates the UI will be much appreciated.

Question by:Vyyk_Drago
LVL 55

Accepted Solution

Jaime Olivares earned 1400 total points
ID: 38362595
The trick is: don't try to add items to your listbox; use binding to an ObservableCollection instead and bind the ListBox.ItemsSource property to that collection.

More info at: http://msdn.microsoft.com/en-us/magazine/dd252944.aspx
LVL 62

Expert Comment

by:Julian Hansen
ID: 38362635
jaime_olivares solution is probably the better one to go for - but as a quick fix and as a matter of interest - does this help?
'Add the files
        For Each strFile As String In strFiles
            clsFile = New FileInfoEx(strFile)

Open in new window

LVL 83

Expert Comment

ID: 38362762
>I get all manner of errors from claims that the backgroundworker does not report progress

Following examples may help


LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 600 total points
ID: 38363446
Not sure what you tried already...

Here is how to do it manually:
    Private Delegate Sub AddFileDelegate(ByVal fiX As FileInfoEx)

    Private Sub AddFile(ByVal fiX As FileInfoEx)
        If Me.InvokeRequired() Then
            Me.Invoke(New AddFileDelegate(AddressOf AddFile), New Object() {fiX})
        End If
    End Sub

Open in new window

Then in your main loop, just call AddFile() instead of accessing the ListBox directly:
        'Add the files 
        For Each strFile As String In strFiles
            clsFile = New FileInfoEx(strFile)

Open in new window

*Note that updating the ListBox "as the files are found" may not be the best idea...it may result in the main UI thread being just as busy as the background thread, negating the whole point of using a thread in the first place.  If you find this to be the case, then either use binding as suggested earlier, or update the ListBox all at once AFTERWARDS.

Author Closing Comment

ID: 38450676
Apologies for the delay in closing this out, but the material took a long time to work through and test to make sure the proposed solutions worked.  Thanks for the help!

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Iteration: Iteration is repetition of a process. A student who goes to school repeats the process of going to school everyday until graduation. We go to grocery store at least once or twice a month to buy products. We repeat this process every mont…
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
If you are looking for an automated tool which can generate reports for Outlook emails and other items from PST file, then you can go for Kernel PST Reporter tool. The reports which are created by this tool are helpful to analyze and understand PST …

587 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question