Search code examples
vb.netbackgroundworker

Vb.Net Background Worker Updating UI Not Working


I have a background worker that is supposed to be updating a ToolStripLabel with some status messages. However, the updating is not happening, but no errors are being thrown. Here's the code I am using:

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

Nothing happens when the ProgressChanged is fired. I've debugged it and it'll print a line to the output window, but it will not update that label. Any ideas on what I'm missing?


Solution

  • You're calling:

    Incident_Form.BackgroundWorker3.ReportProgress()
    

    instead of just:

    BackgroundWorker3.ReportProgress()
    

    Your BackgroundWorker3_ProgressChanged method is subscribed to the ProgressChanged event of the BackgroundWorker located in the current form, not in the Incident_Form form.

    Remove Incident_Form from the beginning of the BackgroundWorker3.ReportProgress() calls and you should be good to go.