Solved

update progress bar in the background visual basic .net

Posted on 2013-01-30
7
1,903 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Getting multiple values from a dataset ASP.NET(VB) 3 37
Please explain "Multi-Tenant Services" 5 65
SQL Login 17 40
Advice on Xojo as a development tool over VB. 4 40
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

867 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

22 Experts available now in Live!

Get 1:1 Help Now