【问题标题】:Pause update of progress bar暂停更新进度条
【发布时间】:2012-07-25 08:24:17
【问题描述】:

(对不起,标题不太好,我不确定我的问题如何解决,所以我需要问什么问题:)

上下文

我使用进度条来通知用户任务的进度。该任务实际上分两个步骤完成,每个步骤大约需要执行总时间的一半。我只知道第二个任务在开始之前的长度(因为它取决于上一个任务的结果),所以我无法知道一开始的最大进度。这就是为什么我在第二个任务之前更改进度条的最大进度。

这基本上是我的做法:

// 1st step
progressBar.Maximum = step1Objects.Count * 2; // "2" because the step will take 
                                              // half of the total process
progressBar.Value = 0;
foreach (SomeObject step1Object in step1Objects) {
    // Build step2Objects
    progressBar.Value = ++progress;
}
// At this moment, the progress bar is half filled

// 2nd step
progressBar.Maximum = step2Objects.Count * 2;
progressBar.Value = step2Objects.Count;
// When we start this step, the progress bar is already half filled
foreach (SomeObject step2Object in step2Objects) {
    // Do something
    progressBar.Value = ++progress;
}
// At this moment, the progress bar is totally filled

问题

当我到达这条线时:

progressBar.Maximum = step2Objects.Count * 2;

...进度条有一小会儿没有填满一半,因为step1Objects.Countstep2Objects.Count 相比非常少。所以进度条做了类似的事情(这就是用户看到的):

>          |
=>         |
==>        |
===>       |
====>      |
=====>     |  End of step 1
=>         |  progressBar.Maximum = step2Objects.Count * 2;
=====>     |  progressBar.Value = step2Objects.Count;
======>    |
=======>   |
========>  |
=========> |
==========>|

问题

如何避免这种“故障”?

我认为要做的是在两个步骤之间停止刷新进度条。我在想BeginUpdate/EndUpdate之类的东西,但进度条似乎不存在......

【问题讨论】:

    标签: c# winforms progress-bar


    【解决方案1】:

    您写道,这两项任务大约是要执行的总时间的一半。

    progressBar.Maximum = 100;
    
    var stepPercentage = 50 / step1Objects.Count;
    foreach(SomeObject step1Object in step1Objects)
    {
        progressBar.Progress += stepPercentage;
    }
    
    progressBar.Progress = 50;
    stepPercentage = 50 / step2Objects.Count;
    foreach(SomeObject step2Object in step2Objects)
    {
        progressBar.Progress += (stepPercentage + 50);
    }
    

    【讨论】:

      【解决方案2】:

      将progressBar的Max设置为step1Objects.Count * step2Objects.Count * 2

      progressBar.Maximum = step1Objects.Count * step2Objects.Count * 2;
      

      然后在你的循环中,尝试:

      progressBar.Value = 0;
      //phase 1
      foreach (SomeObject step1Object in step1Objects) {
          // Build step2Objects
          progress = progress + step2Objects.Count;
          progressBar.Value = progress;
      }
      
      //phase 2
      foreach (SomeObject step2Object in step2Objects) {
          // Do something
          progress = progress + step1Objects.Count;
          progressBar.Value = progress;
      }
      

      【讨论】:

        【解决方案3】:

        使用标准值,例如 100 或 200。现在除以 2。第一步一半,第二步一半。现在您知道第一步有多少项目,因此将 50% 除以该数量以获得第一步中每个对象完成的数量。这意味着一旦第一步完成,进度条的 50% 就已满。现在,接下来的 50% 除以您拥有的第 2 步对象的数量,然后将每个对象的 x 数量添加到您已经填充的 50% 中。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-01-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-03-08
          • 2020-11-05
          • 1970-01-01
          • 2014-04-09
          相关资源
          最近更新 更多