BackgroundWorker 不更新其他形式的 GUI

BackgroundWorker not updating GUI at other Form

如前所述,我有一个运行子函数 DoHeavyWork() 的 BackgroundWorker。

 Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        If BackgroundWorker1.CancellationPending = True Then
            e.Cancel = True
        Else
            'DO HEAVY WORK
            DoHeavyWork()
        End If
    End Sub
 Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
        ProgressBar1.Value = e.ProgressPercentage
        Label8.Text = e.ProgressPercentage.ToString() + " %"
    End Sub


Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted

        If e.Cancelled = True Then
            ProgressBar1.Value = 0
            Label8.Text = ""
        ElseIf e.Error IsNot Nothing Then
            MessageBox.Show(e.Error.Message)
        Else
            MessageBox.Show("Completed!")
        End If
    End Sub

在这个子函数DoHeavyWork() 中,有更新另一个窗体GUI 的代码。

Private Sub DoHeavyWork()
For i As Integer = 1 To fresult_counter
  Dim fresult As New Button
                fresult.Name = "fresult_" & i
                fresult.Text = result(index_acc(i - 1)).ToString

                fresult.TextAlign = ContentAlignment.MiddleLeft
                fresult.Width = 265
                fresult.AutoSize = True
                fresult.BackColor = Color.White

                With fresult.FlatAppearance
                    .BorderColor = Color.White
                    .BorderSize = 2
                    .MouseDownBackColor = Color.DeepSkyBlue
                    .MouseOverBackColor = Color.DeepSkyBlue
                End With
                fresult.Anchor = AnchorStyles.Left
                fresult.FlatStyle = FlatStyle.Flat
                fresult.UseVisualStyleBackColor = False
                fresult.Location = New Point(0, 22 * (i - 1))
                Form1.TabControl2.TabPages(1).Controls.Add(fresult)
                BackgroundWorker1.ReportProgress(i)
 Next
 End Sub

问题是它没有更新 GUI,但进度条在工作。我已经尝试从 BackgroundWorker 中获取代码集并且它工作正常。是不是我没有设置什么来启用 BackgroundWorker 来更新 GUI?

jmcilhinney所述,我的答案的解决方案非常简单。

让BackgroundWorker在后台更新进度条就好了。当它完成它的工作时,进度条将在 100,这意味着它已完成。

完成后,只需在 BackgroundWorker 完成后在子函数中添加更新 GUI 代码(在我的例子中:'Form1.TabControl2.TabPages(1).Controls.Add(fresult)'):

Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted

        If e.Cancelled = True Then
            ProgressBar1.Value = 0
            Label8.Text = ""
        ElseIf e.Error IsNot Nothing Then
            MessageBox.Show(e.Error.Message)
        Else
            MessageBox.Show("Completed!")
            Form1.TabControl2.TabPages(1).Controls.Add(fresult)
        End If
    End Sub