Vb.Net 后台工作人员正在更新 UI 不工作

Vb.Net Background Worker Updating UI Not Working

我有一个后台工作人员应该用一些状态消息更新 ToolStripLabel。但是,更新没有发生,但没有抛出任何错误。这是我使用的代码:

Private Sub BackgroundWorker3_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker3.DoWork
        BackgroundWorker3.WorkerReportsProgress = True
         Dim Counter As Integer = 0

        Do Until BW1Running = False
            Counter = Counter + 1
            Threading.Thread.Sleep(1000)
            Incident_Form.BackgroundWorker3.ReportProgress(Counter)
            If Counter >= 100 Then
                e.Result = False
                Return
            End If

        Loop

        If BW1Running = False Then
            Counter = 100
            Incident_Form.BackgroundWorker3.ReportProgress(Counter)
        End If
    End Sub


Private Sub BackgroundWorker3_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles BackgroundWorker3.ProgressChanged

    Me.ToolStripStatusLabel1.Text = e.ProgressPercentage.ToString

End Sub

触发 ProgressChanged 时没有任何反应。我调试了它,它会在输出 window 中打印一行,但它不会更新该标签。关于我遗漏的任何想法?

您正在呼叫:

Incident_Form.BackgroundWorker3.ReportProgress()

而不仅仅是:

BackgroundWorker3.ReportProgress()

您的 BackgroundWorker3_ProgressChanged 方法订阅了位于 当前表单 而非 Incident_Form 表单中的 BackgroundWorker 的 ProgressChanged 事件.

BackgroundWorker3.ReportProgress() 调用的开头删除 Incident_Form,您应该可以开始了。