Search code examples
vb.netprogress

Progress Bar Value Calculation - Equation


Right so I have 2 progress bars and the equation that I have made to calculate the progress of the second progress bar (The overall progress) in theory should work fine. It works fine for the first download but then when it gets to the second download instead of continuing it jumps to 100 straight away.

Now I am not sure if this is because of my custom progress bar or because of something else.

This is my code for the progress bar.

If CProgressBarCurrent.Value >= 1 Then
    CProgressBarTotal.Value = (100 * (currentFileNumber - 1) + CProgressBarCurrent.Value) / Me.fileUrls.Count
End If

These are the functions:

Dim currentFileNumber As Integer = 1

I add one to it every time a download is finished.

CProgressBarCurrent.Value

Value of my first progress bar which shows how much of the file it has downloaded. Byte wise.

Me.fileUrls.Count

Amount of files in the queue.

I have also tried another equation which was this:

CProgressBarTotal.Value = (currentFileNumber / Me.fileUrls.Count + CProgressBarCurrent.Value / 100 / Me.fileUrls.Count) * 100

This partially works also but it had the same problem. When the first download was completed instead of continuing it would just go to 100%.

I manage this via a timer tick. So for every tick it will do carry the process out.

This is my Progress Bar max value property:

    Property Maximum As Double
    Get
        Return MaxValue
    End Get
    Set(ByVal value As Double)
        If MaxValue < 0 Then MaxValue = 0 Else If MaxValue < MinValue Then MaxValue = MinValue
        MaxValue = value
    End Set
End Property

This is my Value property:

    Property Value As Double
    Get
        Return Percent
    End Get
    Set(ByVal value As Double)
        If value < Minimum Then value = Minimum Else If value > Maximum Then value = Maximum
        Percent = value
        If Percent = 0 Then
            PictureBox1.Width = CInt(value * 3.74)
        Else
            PictureBox1.Width = CInt(value * 3.74 / Maximum * 100)
        End If
    End Set
End Property

My Declarations:

    Protected MinValue As Double = 0.0
Protected MaxValue As Double = 100.0
Protected Percent As Double = 0.0

Thanks.


Solution

  • The correct equation is the following:

    CProgressBarTotal.Value = ((currentFileNumber - 1) + CProgressBarCurrent.Value / 100 ) * 100 / Me.fileUrls.Count
    

    This come from two parts:

    percentOfCompletedFiles  = (currentFileNumber - 1) / Me.fileUrls.Count * 100
    percentCurrentFileReferredAllFiles = CProgressBarCurrent.Value / Me.fileUrls.Count
    

    added toghether and simplifying the Whole.

    Some conditions has to be met in order this to be working:

    • Me.fileUrls maintain also the already downloaded urls in the list
    • CProgressBarCurrent.MaxValue = 100
    • CProgressBarCurrent.Value returns double or float (or integer division issues may occur)