Click here to Skip to main content
15,904,652 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Just today, I posted my attempts at getting a simple Backgroundworker example that wouldn't work, asking for help. The very kind experts here immediately showed me what was wrong and how to fix it. As a result, the code below is a simple example that actually works (unlike most I see on line) and is a good learning start for us lesser mortals (and in VB no less). It takes two buttons, a label and a Progress Bar. Unfortunately, it's not particularly practical and I'm wondering if there isn't a way to have a simple example of the code in a module or class and be able to call it from the other modules or classes. The examples I've checked out go straight to an intermediate level and are quite difficult to understand. I tried adding the code, as is, to a module but got lost in declarations and WithEvents. Is it possible?

What I have tried:

VB.NET
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
        Try
            If BackgroundWorker1.WorkerSupportsCancellation = True Then
                BackgroundWorker1.CancelAsync()
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    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) Handles BackgroundWorker1.DoWork
        Try
            Dim worker As BackgroundWorker = TryCast(sender, BackgroundWorker)

            For i As Integer = 1 To 100
                If worker.CancellationPending = True Then
                    e.Cancel = True
                    Exit For
                Else
                    System.Threading.Thread.Sleep(100)
                    worker.ReportProgress(i)
                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) Handles BackgroundWorker1.ProgressChanged
        Try
            resultLabel.Text = (e.ProgressPercentage.ToString() & "%")
            ProgressBar1.Value = e.ProgressPercentage
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub backgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        Try
            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

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
End Class 
Posted
Updated 29-Aug-23 2:08am
v2
Comments
Ralf Meier 21-Aug-23 17:35pm    
you've got an answer from Dave - but for my understanding : what is the final goal you are facing with this question ?
Member 15070471 21-Aug-23 18:48pm    
As you suggest, I think I have the answer from Dave. It can't be done easily. But as I mentioned above, it seemed impractical to redo this in the main form each time and I was hoping for a way to reuse the same easily understandable code. I'll keep researching and learning. Thank you for your replies.
Ralf Meier 22-Aug-23 5:29am    
Perhaps you could create a Component or a customized Control - but for this it is necessary to know what the goal is. That was the reason for my last comment ...

1 solution

Not as coded. You could wrap it in a class but the code would have to be HEAVILY modified to work properly, like giving it arbitrary work to do and support any possible options, like showing progress in an appropriate control, and supporting cancelation or not.
 
Share this answer
 
v2

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