Progress Bar C#

早过忘川 提交于 2019-12-19 10:32:27

问题


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?


回答1:


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.




回答2:


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++;
}


来源:https://stackoverflow.com/questions/1697168/progress-bar-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!