Link to home
Start Free TrialLog in
Avatar of chadmanvb
chadmanvb

asked on

update progress bar in the background visual basic .net

I have a progress bar I want to run in the background.  I have it display, but it's just blank and does not update.  Here is how I have it setup.

first I call it in a sub with
BackgroundWorker1.RunWorkerAsync()



  Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Dim intCounter As Integer = 0
        ProgressBar1.Maximum = 100

        For x = 0 To 100 - 1

            System.Threading.Thread.Sleep(20)    'set delay for progress bar

            intCounter = intCounter + 1
            BackgroundWorker1.ReportProgress(x)
            Application.DoEvents()  'force progress bar update

        Next


    End Sub

    Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
        ' Here's where you update the progressbar
        ProgressBar1.Value = e.ProgressPercentage
        Application.DoEvents()  'force progress bar update
    End Sub
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Hi chadmanvb,

This line should cause an error, since you are setting a UI control value directly from the background thread:

    ProgressBar1.Maximum = 100

You need to invoke that, or set from the main UI thread before starting the background thread.

The Application.DoEvents() lines should not be necessary.

Other than that, I don't see anything wrong with the code.

With that in mind, can you show the complete forms code?...
Avatar of chadmanvb
chadmanvb

ASKER

I still cant get this to work.  I do see the progress bar come up on the screen and it goes away in a few seconds, but never updates.  Just a white background.  Then when I call the sub again, I see the progress bar is full, stays up a few seconds and goes away.  Not sure why it's not deplaying the movement.

'refresh sub****************************
    Public Sub RefreshStatus()

        'start progressbar
        ProgressBar1.Visible = True
        ProgressBar1.Maximum = 100
        BackgroundWorker1.RunWorkerAsync()

        'here I do some stuff that takes about 2 seconds

ProgressBar1.Visible = False



exit sub



Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Dim intCounter As Integer = 0
        'ProgressBar1.Maximum = 100

        For x = 0 To 100 - 1

            System.Threading.Thread.Sleep(20)    'set delay for progress bar

            intCounter = intCounter + 1
            BackgroundWorker1.ReportProgress(x)
            Application.DoEvents()  'force progress bar update

        Next


    End Sub

    Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
        ' Here's where you update the progressbar
        ProgressBar1.Value = e.ProgressPercentage
        Application.DoEvents()  'force progress bar update
    End Sub
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thats sounds like my issue.  I'm calling a dll to ssh to a linux server, run some commands, and redirect the output to the user on a listview and label.  I'm not sure how to run the other items in the background and then display them on my main form.  Maybe run the progress bar on an other form or this not a good idea?
Well...the simplistic answer is to run those commands in the DoWork() handler of a BackgroundWorker()!  Use either the ProgressChanged() or RunWorkerCompleted() events to pass the info back to the UI.
have you set the WorkerReportsProgress  property to True?

check http://emoreau.com/Entries/Articles/2006/12/The-BackgroundWorker-component.aspx
Thanks, that's what I was doing.