Progress Bar C#
I have a progress bar to show the status of the program loading songs into the library.
foreach (Song s in InitializeLibrary())
{
Libr开发者_如何学Pythonary.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?
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.
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++;
}
精彩评论