Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

update progress bar in the background visual basic .net

Posted on 2013-01-30
7
1,963 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 

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 70

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

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)…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

839 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