[VB .NET] [Controls] [Extension] SmoothProgressBar
I made an extended progressbar which will change it's percentage based on a given time scale (in milliseconds).
vb Code:
Public Class SmoothProgressBar
Inherits ProgressBar
Private _t As Threading.Thread
Private _trunning As Boolean
Private _time As Integer
Private oldproc As Integer
Private newproc As Integer
Private starttime As Date
Public Sub SetProgress(ByVal percentage As Integer, Optional ByVal time As Integer = 200)
Me.Time = time
Me.Value = percentage
End Sub
Public Property Time() As Integer
Get
Return Me._time
End Get
Set(ByVal value As Integer)
Me._time = value
End Set
End Property
Public Overloads Property Value() As Integer
Get
Return newproc
End Get
Set(ByVal value As Integer)
Me.newproc = value
Me.oldproc = MyBase.Value
Me.starttime = Date.Now
If Not _trunning Then
_t = New Threading.Thread(AddressOf UpdateProgress)
_t.IsBackground = True
_t.Start()
End If
End Set
End Property
Private Delegate Sub myadapter(ByVal perc As Integer)
Private Sub adapter(ByVal perc As Integer)
If Me.InvokeRequired Then
Me.Invoke(New myadapter(AddressOf adapter), perc)
Else
MyBase.Value = perc
End If
End Sub
Private Sub UpdateProgress()
_trunning = True
Do
Dim elapsed As TimeSpan = Date.Now.Subtract(starttime)
If elapsed.TotalMilliseconds >= Me.time Then Exit Do
'====================================================
Dim percofperc As Double = elapsed.TotalMilliseconds / Me.time
adapter(oldproc + (newproc - oldproc) * percofperc)
Threading.Thread.Sleep(50)
Loop
adapter(newproc)
_trunning = False
End Sub
End Class
It uses a thread in the background, which will run until it is no longer needed. Usage:
Code:
SmoothProgressBar1.Time = 1000
SmoothProgressBar1.Value = 20
Code:
SmoothProgressBar1.SetProgress(20, 1000)
This can be useful for operations that take long, but don't have intermediate progresses. Say, you are waiting for a connection to a server, you could use the timeout value as time scale for this control.
If a next value change is sent before the previous one is updated, it will simply start from the current value.