I'm having a bit of trouble with progress bars and background workers and want to try and solve this with the simplest solution

What I am doing is reading the contents of a specific directory, I am then displaying in a list box all the folder names so that a user can click on them for further functionality

What I want to do, Is have a progress bar run whilst it is adding the folders to the list box but I am not sure how I can do it..

Is anyone able to help me out?


C# - Visual Studio 2010

Recommended Answers

All 3 Replies

Calculating how many files are in a directory structure (the max of the progress bar) would take pretty much just as much time as loading in the directory structure (which isn't very long with System.IO.Directory.GetDirectories()).

Basically the time it takes to count the directories is almost identical to loading all of the directory strings into an array. So unless you want this operation to take twice as long, I would just use a Marquee progress bar to let the user know the pc is busy. How are you loading in these directory names?

If there is a loop and you try to change ProgressBar value from this loop you have to check is your ProgressBar in the same thread of loop is working. (Use InvokeRequired and if it is true Invoke the method)

I recommend you dont change the value of a ProgressBar or any other controls within a thread. Instead rise an custom event.

Try pInvoke and FindFirst/FindNext for adding files into list.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.