Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

update progress bar in the background visual basic .net

Posted on 2013-01-30
7
Medium Priority
?
2,241 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
[X]
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
  • 3
  • 3
7 Comments
 
LVL 86

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 86

Accepted Solution

by:
Mike Tomlinson earned 2000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 86

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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

609 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