简体   繁体   English

进度栏卡住了C#

[英]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. 进度条卡住了,因为您只调用一次PerformStep When you call PerformStep , the progress bar's value is incremented by 5 (since you set it to 5). 当您调用PerformStep ,进度条的值将增加5(因为您将其设置为5)。

In order for the progress bar to move more, you have to call PerformStep more often. 为了使进度条移动更多,您必须更频繁地调用PerformStep

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. 每当您想显示进度已经完成时,都可以调用PerformStep

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. 每次要更改时,都必须调用PerformStep()。 With the current code, calling PerformStep() 20 times should make it appear completed. 使用当前代码,调用PerformStep()20次应使其看起来已完成。 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. 您可能想要做很长时间的事情,并且要求您在另一个线程(而不是Welcome_Screen_Load线程)上显示该进度条(如果该线程正在处理用户输入和GUI更新),以免您的加载屏幕可能没有响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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