Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Being a bit dumb, I'm struggling to get a simple BackGroundWorker example working so I can start thinking about how to add it to my application. I've tried many examples from online but can't seem to get the BackGroundWorker to start. The following code is the simplest example I can find to get started with, but the DoWork never starts. I don't get any errors either. Please can someone advise what I am doing wrong? Many thanks in advance.

What I have tried:

VB
Imports System.ComponentModel

Public Class Form1
    Private Sub startAsyncButton_Click(sender As Object, e As EventArgs) Handles startAsyncButton.Click

        Try
            If BackgroundWorker1.IsBusy <> True Then
                BackgroundWorker1.RunWorkerAsync()
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

    Private Sub cancelAsyncButton_Click(sender As Object, e As EventArgs) Handles cancelAsyncButton.Click
        If BackgroundWorker1.WorkerSupportsCancellation = True Then
            BackgroundWorker1.CancelAsync()
        End If
    End Sub

    Public Sub New()
        InitializeComponent()
        BackgroundWorker1.WorkerReportsProgress = True
        BackgroundWorker1.WorkerSupportsCancellation = True
    End Sub

    Private Sub backgroundWorker1_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)

        Try
            Dim worker As BackgroundWorker = TryCast(sender, BackgroundWorker)

            For i As Integer = 1 To 10

                If worker.CancellationPending = True Then
                    e.Cancel = True
                    Exit For
                Else
                    System.Threading.Thread.Sleep(1000)
                    worker.ReportProgress(i * 10)
                End If
            Next

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

    Private Sub backgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs)
        resultLabel.Text = (e.ProgressPercentage.ToString() & "%")
    End Sub

    Private Sub backgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs)
        If e.Cancelled = True Then
            resultLabel.Text = "Canceled!"
        ElseIf e.[Error] IsNot Nothing Then
            resultLabel.Text = "Error: " & e.[Error].Message
        Else
            resultLabel.Text = "Done!"
        End If
    End Sub

End Class
Posted
Updated 29-Aug-23 2:34am
v2

1 solution

You're missing the Handles clauses on the DoWork, ProgressChanged, and RunWorkerCompleted event handler function headers:
VB.NET
Private Sub backgroundWorker1_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles backgroundWorker1.DoWork
    Dim worker As BackgroundWorker = TryCast(sender, BackgroundWorker)

    For i As Integer = 1 To 10
        If worker.CancellationPending = True Then
            e.Cancel = True
            Exit For
        Else
            System.Threading.Thread.Sleep(1000)
            worker.ReportProgress(i * 10)
        End If
    Next
End Sub

Private Sub backgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs) Handles backgroundWorker1.ProgressChanged
    resultLabel.Text = (e.ProgressPercentage.ToString() & "%")
End Sub

Private Sub backgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) Handles backgroundWorker1.RunWorkerCompleted
    If e.Cancelled = True Then
        resultLabel.Text = "Canceled!"
    ElseIf e.[Error] IsNot Nothing Then
        resultLabel.Text = "Error: " & e.[Error].Message
    Else
        resultLabel.Text = "Done!"
    End If
End Sub
 
Share this answer
 
Comments
Andre Oosthuizen 21-Aug-23 10:38am    
Thanks Dave, I missed it as well, as you mentioned from the documentation. Your solution is the perfect one, I will remove mine.
Member 15070471 21-Aug-23 11:33am    
You guys are great, thank you so much. I can see now what I missing. (Question is, why the heck did I not see it before lol)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900