0

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

1 Answer 1

1

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
2
  • Just out of interest, won't the calling of this routine distort the amount of time left? (Obviously only negligibly, but still...)
    – user1945782
    Sep 16, 2013 at 8:47
  • Yeah, sure. But for me it does not matter.
    – tmighty
    Sep 19, 2013 at 11:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.