BackgroundWorker不在其他表单中更新GUI

时间:2017-03-13 01:45:01

标签: vb.net visual-studio visual-studio-2012 backgroundworker

如上所述,我有一个运行子函数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?

1 个答案:

答案 0 :(得分:0)

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