在完成后台工作人员的操作之前,进度条重复两三次。
此代码:
Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
'Load Data
For i As Integer = 0 To ListData.Count
BackgroundWorker1.ReportProgress((i / ListData.Count) * 100)
Next
If BackgroundWorker1.CancellationPending Then
e.Cancel = True
End If
End Sub
Private Sub BackgroundWorker1_ProgressChanged(sender As System.Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
Me.ProgressBar1.Value = e.ProgressPercentage
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As System.Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
'set datasource of DatagridView
ProgressBar1.Style = ProgressBarStyle.Blocks
End Sub以我的负载形式
BackgroundWorker1.WorkerReportsProgress = True
BackgroundWorker1.WorkerSupportsCancellation = True
BackgroundWorker1.RunWorkerAsync()
ProgressBar1.Style = ProgressBarStyle.Marquee请帮帮我
发布于 2013-08-21 03:16:28
你犯了几个错误。首先,如果您打算显示一个不断增加的进度条,您应该使用:
ProgressBar1.Style = ProgressBarStyle.Continuous在你的负载表中。接下来,您只在检查完所有的BackgroundWorker1.CancellationPending之后才检查ListData。太晚了,您必须检查循环的每一次迭代。我也很怀疑您是否希望循环从0转到ListData.Count;您可能希望从1开始,或者转到ListData.Count - 1。从你的问题上我看不出来。您的循环应该更像这样:
For i as Integer = 0 To ListData.Count - 1
If BackgroundWorker1.CancellationPending Then
e.Cancel = True
Exit For
Else
' You should be doing some work here, not just calling ReportProgress
BackgroundWorker1.ReportProgress(100 * (i+1) / ListData.Count)
End If
Next另一个错误是计算(i / ListData.Count) * 100;i和ListData.Count是整数,所以它们的除法在结束时总是为零,当它是1时。相反,将分子乘以100得到百分比。
https://stackoverflow.com/questions/18348134
复制相似问题