ProgressBarStyle.Marquee & backgroundWorker is not working RRS feed

  • Question

  • Hallo,

    I´m writing a code that generates up to 10.000 Excel Files in a loop. Normally it will be run within a JobServer, so nobody cares how long it takes, but it also can be run as a Dialog. And there I want to have a ProgressBar. And I have it, but it is not working.

    What I have is:

    Private WithEvents m_backgroundWorker As New System.ComponentModel.BackgroundWorker

        Private myErrorTextArrayCount As Integer = 0
        Private myErrorTextArray As ArrayList
        Private myError As Boolean = False

    within the Dialog Class

    Nothing in _Load

    Code in OK Click Button

       Private Sub btnOk_Click(sender As Object, e As EventArgs) Handles btnOk.Click
            myErrorTextArray = New ArrayList
            Me.pgrbAction.Style = ProgressBarStyle.Marquee
            Me.pgrbAction.MarqueeAnimationSpeed = 100
        End Sub

    Code in Do Work

        Private Sub m_backgroundWorker_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles m_backgroundWorker.DoWork
            ' Debugger.Break()
            myError = myMember.GenerateFile(myParameter, myErrorTextArray)
            myErrorTextArrayCount = myErrorTextArray.Count
        End Sub


        Private Sub BackgroundWorker_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles m_backgroundWorker.RunWorkerCompleted
            Me.pgrbAction.MarqueeAnimationSpeed = 0
                ' Some Code....
        End Sub

    And after all, the ProgressBar is not workinkg. No Movement.

    An Help?

    Tuesday, March 20, 2018 7:33 PM

All replies

  • Here is an example that shows the mechanisms.  The BackgroundWorker must be set to report progress, and there is an event for reporting progress.

    Read this

        Const count As Integer = 1000
        Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
            'simulate work
            For x As Integer = count To 0 Step -1 'backwards for the heck of it
                Threading.Thread.Sleep(10) 'simulate work
        End Sub
        Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
            pgrbAction.Value = e.ProgressPercentage
        End Sub
        Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
            'pgrbAction.Style = ProgressBarStyle.Continuous
            'pgrbAction.MarqueeAnimationSpeed = 100
            pgrbAction.Value = 0
            pgrbAction.Maximum = count
            BackgroundWorker1.WorkerReportsProgress = True
        End Sub

    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it" - MSDN User JohnWein

    Tuesday, March 20, 2018 7:49 PM
  • Maybe the line ‘Me.pgrbAction.MarqueeAnimationSpeed = 0’ is not needed yet.

    Tuesday, March 20, 2018 7:51 PM
  • A background worker runs on a separate thread. The progress changed event is apparently a delegate as it can access the main thread after being called by the do work thread.

    So in the do work thread you want with whatever code is in it to send an update to the progress changed event sub. And in that you can set a number for setting the progress bars value.

    Something like m_backgroundWorker.ReportProgress(Number representing ProgressBars Value) in the Do Work subs code and in the Progress Changed subs code e.ProgressPercentage will return that value then update the progress bar like pgrbAction.Value = e.ProgressPercentage.

    That will keep the code from committing illegal cross thread calls. Both the Progress Changed and RunWorkerCompleted subs can access the main ui without cross threading.

    La vida loca

    Wednesday, March 21, 2018 1:57 AM