0

I'm trying to make the progress bar show the current progress but for some reason the progress bar is just shown there and nothing is happening. I'm using the following code

Private Sub BuildApp_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles BuildApp.ProgressChanged
        Me.ProgressBar1.Value = e.ProgressPercentage
End Sub

I checked if the background worker's property is set to report status and it is set to "Yes". Also, I checked the progress bar's style, it's set to Blocks. Any help?

Krešimir Čulina
  • 93
  • 1
  • 7
  • 19
  • From within the DoWork() handler you need to call ReportProgress() and pass it the current percentage value. Are you doing that?... – Idle_Mind Jul 07 '13 at 23:33
  • So it means I need to manually type in a value after each operation in my bg worker? – Krešimir Čulina Jul 07 '13 at 23:35
  • @KrešimirČulina yes - the background worker cannot magically determine how much work is remaining! – Blorgbeard Jul 07 '13 at 23:40
  • Exactly...the program has NO IDEA what "progress" means to your specific application. You have to call ReportProgress() manually whenever you want the ProgressBar to update. Each call to ReportProgress() results in a corresponding firing of the ProgressChanged() event. – Idle_Mind Jul 07 '13 at 23:40
  • can you show us the code for DoWork()? – jomsk1e Jul 08 '13 at 00:14

1 Answers1

4

Here's a simple example...

Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Button1.Enabled = False
        BuildApp.RunWorkerAsync()
    End Sub

    Private Sub BuildApp_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BuildApp.DoWork
        Dim someTotal As Integer = 12
        For x As Integer = 1 To someTotal
            System.Threading.Thread.Sleep(1000) ' simulated work
            BuildApp.ReportProgress(x / someTotal * 100) ' pass the current percentage value
        Next
    End Sub

    Private Sub BuildApp_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BuildApp.ProgressChanged
        Me.ProgressBar1.Value = e.ProgressPercentage
    End Sub

    Private Sub BuildApp_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BuildApp.RunWorkerCompleted
        MessageBox.Show("Done!")
        Button1.Enabled = True
    End Sub

End Class
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
  • If you don't have a nice loop like the example above by which to base your percentage from, then you simply have to manually place ReportProgress() calls in your code at appropriate places with appropriate values. – Idle_Mind Jul 07 '13 at 23:42
  • Or just use an animated gif if its too hard to estimate the total time. http://stackoverflow.com/questions/13206926/vb-net-progressbar-backgroundworker/13486676#13486676 – Jeremy Thompson Jul 08 '13 at 02:04
  • 1
    If you don't have a good "fit" for concrete progress then just set the ProgressBar Style() property to Marquee. Same thing as an animating GIF... – Idle_Mind Jul 08 '13 at 03:16