Search code examples
vb.netsearchrecursionbackgroundworkervb.net-2010

How to I search for files recursively while updating a progress bar and returning the file List to the application from BackgroundWorker


Thanks for reading!

I am currently trying to develop an application which will backup large folders to a specified destination. To find all the files in the specified 'backup' directory I am using the following code.

Public Shared Function GetFilesRecursive(ByVal initial As String) As List(Of String)

    Dim result As New List(Of String)
    Dim stack As New Stack(Of String)
    stack.Push(initial)

    Do While (stack.Count > 0)

        Dim dir As String = stack.Pop
        Try

            result.AddRange(Directory.GetFiles(dir, "*.*"))


            Dim directoryName As String
            For Each directoryName In Directory.GetDirectories(dir)
                stack.Push(directoryName)
            Next

        Catch ex As Exception
            'stay quiet
        End Try
    Loop


    Return result

End Function

From here I am using BackgroundWorker to copy each file and report the progress of the completed list to the GUI via a progress bar.

This is fine and works great until I come to a large directory, say C:\Windows where it hangs and freezes the GUI until it completes, this is horrible!

I have tried putting my GetFilesRecursive function into a separate background worker to run first so I can update the GUI however I am struggling on how to return the List of found files back to my application (I get cross-threading exception) and how to update the progress bar to show the progress of the GetFileRecursive function so the user knows it is processing the list of files and has not crashed/frozen.

Thank you very much in advance for your input! :)

Steve


Solution

  • For reference, here's the BackgroundWorker manual page.

    First, have a look at the RunWorkerAsync(Object) method. With this method, you can pass an object to your 'worker' function. This might be an empty stack, for example.

    In your RunWorkerCompleted event handler, you can grab that same object (now populated with file paths) from the RunWorkerCompletedEventArgs and do what you will with it. The reason I suggest passing an object is that you avoid the need for a static variable. In this case, a static variable is a bit of a code smell and should be avoided.

    If you want to report the progress of the routine, you need to call the ReportProgress(int) function from within your worker function. This will fire the ProgressChanged event which you can use to drive your progress bar.

    MSDN also has a pretty good example of the pattern here. They don't do anything to demonstrate progress reporting but there's an example of that on the ReportProgress page.

    Edit:

    If it's a plain Stack you're using, then I suggest creating the stack, then passing it to RunWorkerAsync( ). From here, you can get the stack from the event args of BackgroundWorker_DoWork, which you can then pass to your actual worker function, in this case, GetFilesRecursive( ). So GetFilesRecursive( ) does not actually return anything, but it's able to fill the stack, which you can then access again from your ProgressCompleted handler. Mind you, you'll need to cast that object back to a Stack (or whatever type of object you decide to use) before you actually use it.

    Big picture: the stack is like a bucket which you pass around, and gets filled. You first create the bucket when you call RunWorkerAsync( ), passing the bucket as an argument.