Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6920391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:08:27+00:00 2026-05-27T10:08:27+00:00

Thanks for reading! I am currently trying to develop an application which will backup

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:08:28+00:00Added an answer on May 27, 2026 at 10:08 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Thanks for reading. I'm a bit new to jQuery, and am trying to make
Thanks for reading this. I am dynamically generating some data which includes a select
Thanks for reading this I thought I could use find(), but couldn't make it
and thanks for reading. I am building a data entry form. I am trying
I'm currently reading the Mathematica Guidebooks for Programming and I was trying to work
I am trying to create an application which allows background threads to update core
Thanks for reading this. I have markup similar to what is below. Using the
Thanks for reading this. I would have thought it would be as simple as
Thanks to reading about error handling on StackOverflow , I discovered Mz-Tools. However, I
Thanks in Advance for reading and answer this question. I got button in asp

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.