Reputation: 11399
I always wanted a simple system that would show me how many minutes approximately are left while processing a long for-next-statement.
I tried do achieve this by creating a class.
At the start, I want to set how many for-nexts I have (-> setMax) At each For-Next, I will tell the class that one of the for-nexts was done. (->addOneDone) The class would then tell me how many minutes I still have to wait until the entire for-next-statement will be done.
I was pretty sure I made that quite ok, but something is still wrong. I suspect it is the milliseconds to minutes conversion.
Would anybody try to help me find my mistake?
Thank you very much!
Option Explicit
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private m_lMax&
Private m_lItemsDone&
Private m_lStart&
Private m_cMillisecondsAlreadyNeeded As Currency
Private m_lMinutesLeft&
Private m_lLastTick&
Public Sub setMax(ByVal uCount As Long)
m_lMax = uCount
m_lStart = GetTickCount()
End Sub
Public Sub addOneDone()
m_lItemsDone = (m_lItemsDone + 1)
Dim lTick&
lTick = GetTickCount
Dim lMillisecondsNeeded&
lMillisecondsNeeded = (lTick - m_lLastTick)
If (m_lLastTick > 0) Then
m_cMillisecondsAlreadyNeeded = (m_cMillisecondsAlreadyNeeded + lMillisecondsNeeded)
Dim lMillisecondsForOneItem&
lMillisecondsForOneItem = (m_cMillisecondsAlreadyNeeded / m_lItemsDone)
Dim lItemsLeft&
lItemsLeft = (m_lMax - m_lItemsDone)
Dim cMillisecondsLeftToBeDone As Currency
cMillisecondsLeftToBeDone = (lMillisecondsForOneItem * lItemsLeft)
Dim lMinutes&
lMinutes = MillisecondsToMinutes(cMillisecondsLeftToBeDone)
m_lMinutesLeft = lMinutes
End If
m_lLastTick = lTick
End Sub
Private Function MillisecondsToMinutes(ByVal uMilliseconds As Long) As Integer
Dim seconds As Double
seconds = uMilliseconds / 1000
Dim minutes As Double
minutes = seconds / 60
MillisecondsToMinutes = minutes
End Function
Public Property Get MinutesLeft() As Long
MinutesLeft = m_lMinutesLeft
End Property
Upvotes: 0
Views: 157
Reputation: 11399
I changed my code, and it works now:
Option Explicit
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private m_lMax&
Private m_lItemsDone&
Private m_lStart&
Private m_cMillisecondsAlreadyNeeded As Currency
Private m_lMinutesLeft&
Public Sub setMax(ByVal uCount As Long)
m_lMax = uCount
m_lStart = GetTickCount()
End Sub
Public Sub addOneDone()
m_lItemsDone = (m_lItemsDone + 1)
Dim lTick&
lTick = GetTickCount
Dim lMillisecondsNeeded&
lMillisecondsNeeded = (lTick - m_lStart)
Dim dblMillisecondsForOneItem As Double
dblMillisecondsForOneItem = (lMillisecondsNeeded / m_lItemsDone)
Dim lItemsLeft&
lItemsLeft = (m_lMax - m_lItemsDone)
Dim cMillisecondsLeftToBeDone As Currency
cMillisecondsLeftToBeDone = (dblMillisecondsForOneItem * lItemsLeft)
Dim lMinutes&
lMinutes = MillisecondsToMinutes(cMillisecondsLeftToBeDone)
m_lMinutesLeft = lMinutes
End Sub
Private Function MillisecondsToMinutes(ByVal uMilliseconds As Long) As Integer
MillisecondsToMinutes = uMilliseconds / 1000 / 60
End Function
Public Property Get MinutesLeft() As Long
MinutesLeft = m_lMinutesLeft
End Property
Upvotes: 1