【问题标题】:ProgressBar not visually showing all stepsProgressBar 没有直观地显示所有步骤
【发布时间】:2017-01-19 09:44:30
【问题描述】:

我制作了一个在 Autodesk Inventor 中创建 3D 模型的应用程序。我想添加一个进度条来向用户显示该过程已经完成了多少。

我遇到的问题是,当 Autodesk Inventor 中的进程消耗大量 CPU 时,进度条没有显示所有步骤,而是跳转(例如进一步 5 步)到最后。

有没有办法强制 windows 窗体显示所有步骤?还是这种行为不常见?

Private Sub
    ' Display the ProgressBar control.
    pbBuildProgress.Visible = True
    ' Set Minimum to 1 to represent the first file being copied.
    pbBuildProgress.Minimum = 1
    ' Set Maximum to the total number of files to copy.
    pbBuildProgress.Maximum = BodyComponents.Count
    ' Set the initial value of the ProgressBar.
    pbBuildProgress.Value = 1
    ' Set the Step property to a value of 1 to represent each file being copied.
    pbBuildProgress.Step = 1


    ' Start loop trough all body components
    For i = 0 To BodyComponents.Count - 1

        ' Some code here that does stuff in Autodesk Inventor

        ' Perform a step
        pbBuildProgress.PerformStep()

    Next
End Sub

【问题讨论】:

    标签: vb.net progress-bar


    【解决方案1】:

    您应该考虑使用BackgroundWoker 报告进度:

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        ProgressBar1.Minimum = 0
        ProgressBar1.Maximum = 10
    
        BackgroundWorker1.WorkerReportsProgress = True
        BackgroundWorker1.RunWorkerAsync()
    
    End Sub
    
    Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    
        For i = 0 To 10
    
            Debug.Write(i)
    
            BackgroundWorker1.ReportProgress(i)
    
        Next
    
    End Sub
    
    Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
    
        ProgressBar1.Value = e.ProgressPercentage
    
    End Sub
    

    这不是我需要做的事情,如果有更多经验的人有任何意见,我会非常乐意学习一两件事。

    【讨论】:

      【解决方案2】:

      尝试在 .PerformStep() 之后更新您的进度条或表单。但是请注意,这需要很多时间,尤其是当您的最大值很大时。例如,也许您可​​以使用秒表并每 250 毫秒更新一次表单。

      【讨论】:

      • 您能解释一下吗?什么需要很多时间?表格的更新?那么添加多个(可能是不必要的)更新应该如何提供帮助?
      • 绘制/更新表单需要很长时间。您应该防止表单进行不必要的更新,而不是强制更新;-) 例如,尝试使用 StopWatch 并仅每 250 毫秒执行一次更新(可能使用 Application.DoEvents)。这将防止您的应用程序冻结。
      • A Stopwatch 用于测量某事所花费的时间,而不是每 x 毫秒运行一次代码。你不应该使用Application.DoEvents()!这样做几乎总是不好的做法。请参阅:Keeping your UI Responsive and the Dangers of Application.DoEvents
      【解决方案3】:

      一个常见的原因是 UI 线程的调用过于频繁,超出了它的处理能力。

      对此的简单解决方法是量化你的进度百分比,下面我运行一个迭代 10 亿次但仅每 1000 万次迭代或 1% 发送一次 UI 更新的操作。

      代码:

      var steps = 1000000000;
      var step = 1.0d / steps;
      var percent = 0;
      for (var i = 0; i < steps; i++)
      {
          var percent1 = (int) (Math.Floor(i * step * 100));
          if (percent1 > percent)
          {
              // TODO invoke your UI progress bar update here
              Console.WriteLine("Updating: percent = {0}, i = {1}", percent1, i);
              percent = percent1;
          }
      }
      

      结果:

      Updating: percent = 1, i = 10000000
      Updating: percent = 2, i = 20000000
      Updating: percent = 3, i = 30000000
      Updating: percent = 4, i = 40000000
      Updating: percent = 5, i = 50000000
      Updating: percent = 6, i = 60000000
      Updating: percent = 7, i = 70000000
      Updating: percent = 8, i = 80000000
      Updating: percent = 9, i = 90000000
      Updating: percent = 10, i = 100000000
      Updating: percent = 11, i = 110000000
      Updating: percent = 12, i = 120000000
      Updating: percent = 13, i = 130000000
      Updating: percent = 14, i = 140000000
      Updating: percent = 15, i = 150000000
      Updating: percent = 16, i = 160000000
      Updating: percent = 17, i = 170000000
      Updating: percent = 18, i = 180000000
      Updating: percent = 19, i = 190000000
      Updating: percent = 20, i = 200000000
      

      您可能还希望异步运行此类操作:

      Task-based Asynchronous Programming

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-02-04
        • 2013-06-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多