title: How to I search for files recursively while updating a progress bar and returning the file List to the application from BackgroundWorker
body: <p>Thanks for reading!</p>
  
  <p>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.</p>
  
  <pre><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 &gt; 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
  </code></pre>
  
  <p>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.</p>
  
  <p>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! </p>
  
  <p>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.</p>
  
  <p>Thank you very much in advance for your input! :)</p>
  
  <p>Steve</p>