我希望能为我的背景工作者提供一些帮助,因为我无法让它工作。基本上我有一个背景工作者,它完成一些任务,但我正在尝试实现进度条和标签。我要将进度条的最大值设置为10,然后随着每个任务的移动更新它,我也想到一个带有进度条的标签,它会显示进度,即
Pb value = 2
20% completed
Pb value = 6
60% completed
等等。
到目前为止,我已将其用于编码。
更新*感谢您的帮助。
所以只是一个简单的问题,做工作事件会是这样吗?
Private Sub BGWorker_DoWork(ByVal sender As Object, _
ByVal e As DoWorkEventArgs) _
Handles BGWorker.DoWork
For i as Integer = 1 to 2
'My task code 1
BGWorker.ReportProgress(i)
Next
For i as Integer = 3 to 4
'My task code 2
BGWorker.ReportProgress(i)
Next
For i as Integer = 5 to 6
'My task code 3
BGWorker.ReportProgress(i)
Next
For i as Integer = 7 to 8
'My task code 4
BGWorker.ReportProgress(i)
Next
For i as Integer = 9 to 10
'My task code 5
BGWorker.ReportProgress(i)
Next
End Sub
答案 0 :(得分:5)
您需要让BackGroundWorker“报告”它的进度,而不是尝试在DoWork
(未在UI线程上运行)中设置进度条值:
Private Sub BGWorker_DoWork(ByVal sender As Object, _
ByVal e As DoWorkEventArgs) _
Handles BGWorker.DoWork
For i as Integer = 1 to 10
BGWorker.ReportProgress(i)
Next
End Sub
然后在ProgressChanged
事件中,您可以设置进度条的值:
Private Sub BGWorker_ProgressChanged(ByVal sender As Object, _
ByVal e As ProgressChangedEventArgs) _
Handles BGWorker.ProgressChanged
Me.Pb1.Value = e.ProgressPercentage
End Sub
答案 1 :(得分:4)
你有几个问题:
BackgroundWorker
。ProgressChanged
事件处理程序。您必须执行所有这些操作才能使用BackgroundWorker
。一本好的初学者教程是http://msdn.microsoft.com/en-us/library/cc221403%28v=vs.95%29.aspx