ProgressBar无法直观显示所有步骤

时间:2017-01-19 09:44:31

标签: vb.net progress-bar

我制作了一个在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

3 个答案:

答案 0 :(得分: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

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

答案 1 :(得分:0)

尝试在.PerformStep()之后更新您的进度条或表单。但要注意,这需要花费很多时间,特别是当你的最大值很大时。也许您可以使用StopWatch并每隔250毫秒更新一次表单。

答案 2 :(得分:0)

一个常见的原因是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