简体   繁体   English

C#进度条未更新

[英]C# progress bar not updating

I have a progress bar on my form, I want it to update everytime at the end of a loop. 我的表单上有一个进度条,我希望它在循环结束时每次都更新。

progressBar1.Value += (progressBar1.Maximum/4);
if (progressBar1.Value == progressBar1.Maximum)
{
  MessageBox.Show("Done!");
}

I have this at the end of my loop, but its not updating as the loop goes, just stays as a solid grey bar...Any tips? 我在循环的末尾有这个,但是它不会随着循环的进行而更新,只是保持为灰色实线...有什么提示吗? Thanks 谢谢

This is not really an answer, and I'll delete it later, but here's an example of minimal, reproducible code 这不是一个真正的答案,我稍后将其删除,但这是一个最少的,可复制的代码的示例

Drop a progress bar and a button on a form, and copy this into the button click event. 在表单上放置进度条和按钮,然后将其复制到按钮单击事件中。 When you click the button, the progress bar shows progress and then a message box is supposed to let you know when it's done, but instead I'm getting the following exception: 当您单击按钮时,进度条将显示进度,然后应该显示一个消息框让您知道完成的时间,但是我收到了以下异常:

An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in System.Windows.Forms.dll System.Windows.Forms.dll中发生了类型为'System.ArgumentOutOfRangeException'的未处理异常

private void button1_Click(object sender, EventArgs e)
{
    progressBar1.Maximum = 999;

    while (progressBar1.Value < progressBar1.Maximum)
    {
        Thread.Sleep(500);
        progressBar1.Value += (progressBar1.Maximum / 4);
    }

    MessageBox.Show("Done!");
}

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

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