views:

582

answers:

2

I have a progress bar to show the status of the program loading songs into the library.

    foreach (Song s in InitializeLibrary())
    {
        Library.AddSong(s);
        pBar.Value++;
        pBar.Update();
    }

InitializeLibrary() is just a function that returns a List

The problem is that the progress bar stops "moving" after a certain point (eg 20%), while the value still increases. Is there a way to make it update 100% of the time?

+8  A: 

The way I have done this is by using a BackgroundWorker component.

Use it to load your songs on a background thread and report progress to the UI thread that will update your progress bar.

The background worker handles all of the messaging between threads for the reporting of progress.

This gets you the benefits of Multithreading without having to manage the threading yourself.

A good tutorial that will show how to use the progress reporting is here.

Jason w
+1, Good idea but it goes way beyond fixing a Progressbar.
Henk Holterman
it has happened to me that when the app is doing lots of "work" on th e UI thread, sometimes the screen doesn't get repainted often and has that "Hanging" feeling. if you seperate the work onto another thread, the painting of the screen is no problem.
Jason w
+1  A: 

You need to set the Maximum property of the progress bar so that it can calculate percentages when you increment the Value:

var items = InitializeLibrary();
pBar.Maximum = items.Length;
foreach (Song s in items)
{
    Library.AddSong(s);
    pBar.Value++;
}
Darin Dimitrov
The maximum is already set, I just didn't show it. Sorry
Mike