我在VB.Net一个进度条和BackgroundWorker的,我想下面给出不同形式的工作:
Form1()
{
MaxRows = 10
for i = 0 to MaxRows then
// Update my value on the progressbar
....
next
}
ProgressBarForm
Private Sub ProgressBarForm_Shown(sender As Object, e As EventArgs) Handles Me.Shown
TransferProgressBar.Visible = True
ProgressBarBackgroundWorker.RunWorkerAsync()
End Sub
Private Sub ProgressBarBackgroundWorker_DoWork(sender As Object, e As ComponentModel.DoWorkEventArgs) Handles ProgressBarBackgroundWorker.DoWork
For i = 0 To TransferProgressBar.Maximum
'Dim Percentage As Integer = Math.Round(((i / (TransferProgressBar.Maximum - TransferProgressBar.Minimum)) * 100))
ProgressBarBackgroundWorker.ReportProgress(i / 100)
Next
End Sub
Private Sub ProgressBarBackgroundWorker_ProgressChanged(sender As Object, e As ComponentModel.ProgressChangedEventArgs) Handles ProgressBarBackgroundWorker.ProgressChanged
TransferProgressBar.Value = e.ProgressPercentage
PercentageLabel.Text = "Processing....." & TransferProgressBar.Value.ToString() & "%"
End Sub
Private Sub ProgressBarBackgroundWorker_RunWorkerCompleted(sender As Object, e As ComponentModel.RunWorkerCompletedEventArgs) Handles ProgressBarBackgroundWorker.RunWorkerCompleted
MsgBox("Task Completed!")
Me.Close()
End Sub
我如何使用BackgroundWorker从另一个窗体/子更新我的进度值? 请让我知道。 我变得有点困惑在这里。
你似乎有回正面。 而不是BackgroundWorker
处理ProgressBar
更新,请使用BGW做费时的工作。 然后每隔一段时间就可以提供使用内置的进度/表更新ReportProgress
方法。
通常情况下,你不能(直接)访问UI控件,就像一个ProgressBar
,从比它上(即UI线程)创建的其他线程。 该ReportProgress
事件引发原/ UI线程上进行基本的进展情况容易。
但是,几乎仅限于这一点。 做多,为漫长的过程的实例结果传递给一个ListBox
,你可以使用一个代表来更新其他/ UI线程的控制。
与长时间运行的任务表单
Private WithEvents bgw As New BackgroundWorker
Private frmProg As ProgForm ' progress bar form
' start up
Private Sub Button1_Click(sender ...etc
' set up
bgw.WorkerReportsProgress = True
bgw.WorkerSupportsCancellation = True
If frmProg Is Nothing Then ' make sure progress form is instanced
ProgForm = New frmProg
End If
If bgw.IsBusy = False Then
frmProg.Show()
bgw.RunWorkerAsync(10) ' do some important work x10
End If
End Sub
' the job that will take a while
Private Sub bgw_DoWork(sender As Object, e As DoWorkEventArgs)
Handles bgw.DoWork
' ToDo: with multiple workers use sender, not 'bgw'
' get the amount of work to do
Dim numToDo As Integer = CInt(e.Argument)
' important, time consuming work done here
For n As Integer = 1 To numToDo
' do the "work"
System.Threading.Thread.Sleep(100)
' post a notice, passing the percentage (raises the ProgressChanged event)
bgw.ReportProgress(Convert.ToInt32((n / numToDo) * 100)
Next
End Sub
' event raised from DoWork via ReportProgress
Private Sub bgw_ProgressChanged(sender As Object, e As ProgressChangedEventArgs)
Handles bgw.ProgressChanged
' method added on the Progress form to
' receive percentage and update the meter:
frmProg.UpdateProgress(e.ProgressPercentage)
' if the progress bar was on the same form,
' update it directly:
'MyProgBar.Value = MyProgBar.Maximum
'MyProgBar.Value = pct
End Sub
' optional event raised when the long running task is complete
Private Sub bgw_RunWorkerCompleted(sender As Object,
e As RunWorkerCompletedEventArgs) Handles bgw.RunWorkerCompleted
' when all done, report that too
MessageBox.Show("Work Complete!")
End Sub
需要注意的是,通常使用Thread.Sleep
冻结UI。 因为它是这并不在这里发生BackgroundWorker
不是其处于睡眠状态的UI线程。
进度条的形式为:
Public Sub UpdateProgress(pct As Integer)
' ToDo: Add error checking
progress.Value = progress.Maximum
progress.Value = pct
End Sub