locked
Background worker percentage feedback RRS feed

  • Question

  • Hi

    I have moved all of my data collection class events from my form load to a background worker. I see that there is an option to get progress feedback. When having an undisclosed amount of work to do in a background worker, does it know its own progress from the amount of work it has to report back or is this something that is still requires building?

    From docs.microsoft it shows that the progress is generated (or at least thats how i read it) in this sub

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

    the full code here

     Public Partial Class Form1
            Inherits Form
    
            Public Sub New()
                InitializeComponent()
                backgroundWorker1.WorkerReportsProgress = True
                backgroundWorker1.WorkerSupportsCancellation = True
            End Sub
    
            Private Sub startAsyncButton_Click(ByVal sender As Object, ByVal e As EventArgs)
                If backgroundWorker1.IsBusy <> True Then
                    backgroundWorker1.RunWorkerAsync()
                End If
            End Sub
    
            Private Sub cancelAsyncButton_Click(ByVal sender As Object, ByVal e As EventArgs)
                If backgroundWorker1.WorkerSupportsCancellation = True Then
                    backgroundWorker1.CancelAsync()
                End If
            End Sub
    
            Private Sub backgroundWorker1_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
                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(500)
                        worker.ReportProgress(i * 10)
                    End If
                Next
            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
    Thanks


    I am here to ask questions and learn from others. Cor Ligthert DO NOT REPLY TO ANY OF MY POSTS!!

    Monday, August 10, 2020 3:22 AM

All replies

  • Hello,

    With an undisclosed amount of work is going to make calculating progress a "pot shot". 

    For example, working with downloading files, you need to figure out each file size and total file size along with calculating speed of the router. Or if working with a database, reading data, you might look at record count of records expected to load and perform a calculation. Then we can look at say how Windows handles specific operations with a progress bar, a long running operation can vary and adjust according to various factors.

    Most likely you seen something like this post but in case you have not it's a basic example.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, August 10, 2020 11:08 AM