Solved

update progress bar in the background visual basic .net

Posted on 2013-01-30
7
1,852 Views
Last Modified: 2013-02-04
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
0
Comment
Question by:chadmanvb
  • 3
  • 3
7 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 38836381
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?...
0
 

Author Comment

by:chadmanvb
ID: 38837019
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
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 38837059
Wait a minute...

In RefreshStatus(), you have this line:

        'here I do some stuff that takes about 2 seconds

Since you're firing off the BackgroundWorker() from this thread, I can only assume that method runs in the main UI thread.

If you have a task that takes 2 seconds, and it runs on the main UI thread, then NOTHING will be processed by main UI thread until after those two seconds are up.  This includes updating UI controls.
You need to move that "stuff" that takes two seconds to the background thread (or change the way it does what it does!).

What "stuff" is going on in there?...
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:chadmanvb
ID: 38837084
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?
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 38837097
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.
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 38837306
have you set the WorkerReportsProgress  property to True?

check http://emoreau.com/Entries/Articles/2006/12/The-BackgroundWorker-component.aspx
0
 

Author Closing Comment

by:chadmanvb
ID: 38850896
Thanks, that's what I was doing.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now