简体   繁体   中英

Progress bar stuck c#

i wanted to make progress bar in my form, but when i run it, the progress bar stuck and is not moving to the end, it stop not even half way.

Any solution?

Thank you.

Here is the code:

private void WelcomeScreen_Load(object sender, EventArgs e)
        {
            progressBar1.Minimum = 0;
            progressBar1.Maximum = 100;
            progressBar1.Step = 5;
            progressBar1.PerformStep();
        }

The progress bar is stuck because you only call PerformStep once. When you call PerformStep , the progress bar's value is incremented by 5 (since you set it to 5).

In order for the progress bar to move more, you have to call PerformStep more often.

For instance, if you have a background worker thread that does work in the background, you want the user to be notified how long the work'll take. You can call PerformStep every time you feel you want to show that progress has been made.

Sample code:

...
progressBar1.Step = 5;

InitTheWork();

progressBar1.PerformStep(); //This increments the pb's value by 5, since property `Step` has been set to 5

DoSomeHeavyWork();

progressBar1.Value += 20; //This increments pb's value by 20

FinalizeWork();

progressBar1.Value = 100; //This fully fills up the progress bar, to signify that the work has been completed.

It is stuck as you are not changing it's value. To make it unstuck change it's value.

The progress bar will not "move" by itself. You have to call PerformStep() every time you want it to change. With the current code, calling PerformStep() 20 times should make it appear completed. You have to find out for yourself at what points in the resource-heavy process it is sensible to update the progressbar. It could look something like this:

PerformTask1();
progressBar1.PerformStep();
PerformTask2();
progressBar1.PerformStep();
PerformTask3();
progressBar1.PerformStep();
...

You will probably want to do whatever is taking a long time and requiring you to show that progressbar in another thread than the one that calls Welcome_Screen_Load if this is the same thread that is handling user input and GUI updates, lest your loading screen may appear unresponsive.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM