Share via


problems with the progress bar updating

Question

Friday, April 8, 2011 9:53 PM

hello all. Pardon me if this is already been answered but I did a search on progress bar and there are over 5 million results. So rather than spend the rest of my life searching all asked this question again. I have a VB.net application using a Windows forms progress bar. The problem is I can set the maximum to a value and when that value is reached the progress bar is not completed. Sometimes it's halfway sometimes is three quarters of the way but never completely filled. Anyone have any answers to this?

Thanks,

pcgalen

All replies (6)

Friday, April 8, 2011 10:19 PM

This is probably due to the fact that there are many things happening on the main thread concurrently and the painting of the progress bar is not given the priority by the system resources.  You can help this by using a System.ComponentModel.BackgroundWorker object to perform the intensive operation that requires the progress bar in the first place.  There are probably...  well, five million example of using a background worker online -- the MSDN example for the class is a good start:  http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx

If you are already using a background worker but are still having issues or else you don't want to attempt it you could try something a little brute force which is to explicitly ask the progress bar to update itself.  This can be done by calling the Refresh() method on teh progress bar.  In fact, this function can be found on all controls and is a much preferred and more surgical technique to the more problematic Application.DoEvents call which clears the entire message queue for all the controls (and more).


Saturday, April 9, 2011 1:17 AM | 1 vote

 The problem is I can set the maximum to a value and when that value is reached the progress bar is not completed. Sometimes it's halfway sometimes is three quarters of the way but never completely filled.

That looks like your progressbar step isn't set properly. Let's say your progress bar maximum value is 100, then you run a task 100 times, then your step value should add 1 each time. If you add a smaller value,then your problem could be ocurred.

Can you paste some code here? That will help you get answer more easily.

 


Saturday, April 9, 2011 3:26 PM

Hi,

Because you didn't showed your progressbar code and we couldn't see the problem.

I created a small progressbar example:

Public Class Form1

 

 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

  ProgressBar1.Maximum = 200

 End Sub



 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

  ProgressBar1.Value = 0

  Timer1.Start()

 End Sub



 Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick





  If ProgressBar1.Value <= 199 Then

   ProgressBar1.Value = ProgressBar1.Value + 1

  Else

   Timer1.Stop()



  End If





 End S

Saturday, April 9, 2011 10:06 PM

hi guys. Thanks for taking the time to reply to this. Below is an example of the where I'm updating the progress bar. I have the step set to one. Here is one example of the code. I'm calling do events between a each invocation which should allow the progress bar and update itself. This application is not multithreaded so I don't believe it's a threading issue although a background worker might help. Anyway here's the code:

 

 Call oUtil.ShowProgressBar(True)
        frmMain.ProgressBar.Maximum = oCustList.Count - 1
        frmMain.ProgressBar.Value = 0
        Call oUtil.SetStatusMessage("Importing Customers...")

        For i As Integer = 0 To oCustList.Count - 1
            frmMain.ProgressBar.Value = i
            Application.DoEvents()
            Dim oCust As ICustomerRet = oCustList.GetAt(i)

            If Not AddQBCustomer(oCust) Then
                Call oUtil.ShowProgressBar(False)

                If Not m_SyncAll Then
                    m_oCn.Close()
                    m_oCn.Dispose()
                    oSM.CloseConnection()
                    Return False
                    Exit Function
                Else
                    Return False
                    Exit Function
                End If
            End If
        Next

 

Thanks again for your help.

 

Regards,

pcgalen


Wednesday, February 27, 2013 7:12 AM

Hi Dig-Boy,

Nice suggestion to use the refresh on the progressbar.  I actually just posted a similar issue and tried your suggestion which worked great! Instead of it making it to 50 to 60% it now appears to go to about 90 to 95% however I noticed there is a price to pay using the refresh method.  It actually slowed down the reading of the file about 3 times longer to read through it all. Any thoughts about this?

Thanks,

Les


Wednesday, February 27, 2013 6:51 PM

Hi Dig-Boy,

Nice suggestion to use the refresh on the progressbar.  I actually just posted a similar issue and tried your suggestion which worked great! Instead of it making it to 50 to 60% it now appears to go to about 90 to 95% however I noticed there is a price to pay using the refresh method.  It actually slowed down the reading of the file about 3 times longer to read through it all. Any thoughts about this?

Thanks,

Les

Hi Les.  Glad you were helped.  However, you are encountering what was the main problem the OP encountered originally - that the UI thread is doing too much (i.e. the IO operations in addition to painting the UI).  Only one thing can happen at a time on a single thread and so the refresh calls are impeding the IO operations because it elevates the priority to the progress bar painting rather than the IO stuff.

So the solution is the same as above.  Use a background worker to perform the IO operation on a different thread and report progress through its built in ReportProgress event.  Then you might even achieve 100% :)

Blog: http://codemidden.wordpress.com