Hi,

I am new to VB6 programming and have some difficulties...

Here is my question:

I have a For Loop running, wherein there should be some communicating with a serial port to get new data (will be implemented later), updating a data array and the corresponding graph.
This works all fine...

But then: When I apply a delay within the For Loop to wait some time (about 1 second) for the next loop iteration to start I get a 'Runtime error: Overflow' message.

How can I avoid this?

Thanks in advance!


My code:

Private Declare Function GetTickCount Lib "kernel32" () As Integer
Private Declare Sub Sleep Lib "kernel32" (ByVal ms As Integer)

Public Sub Delay(ByVal delayms As Integer)
    Dim delayTime As Long
    delayTime = GetTickCount + delayms
    
    Do Until GetTickCount >= delayTime
        Sleep 10
        DoEvents
    Loop
End Sub

Public Sub GetData()
    Dim i As Integer
    Dim Start, Finish, Duration As Integer
    
    For i = 1 To End
        Start = GetTickCount
        
	' Get new Data         
	...
	' Refresh DataArray
        ...
        ' Refresh Graph
        ...
        
        Finish = GetTickCount
        Duration = Start - Finish
        'Delay: Loop repetition after 1s
        Delay (1000 - Duration)
    Next i
End Sub

For i = 1 To End >>> what is END?
check this limit.

the limit is formerly defined (not shown in the excerpt of my code) as a value taken from another array.

Again: The program works fine without the delay...

You could possibly be passing a negative value if your process takes longer than a second (Delay 1000 - Duration). Second, "End" is an execution command so perhaps you should change the declaration MyEnd or something else.

Good Luck

the process takes definitely less than one second and I just placed End here as the variable for short - in my code it's called EndSetRows... (sorry for confusion)

Private Declare Function GetTickCount Lib "kernel32" () As Integer
Private Declare Sub Sleep Lib "kernel32" (ByVal ms As Integer)

Public Sub Delay(ByVal delayms As Integer)
    Dim delayTime As Long
    delayTime = GetTickCount + delayms

    Do Until GetTickCount >= delayTime
        Sleep 10
        DoEvents
    Loop
End Sub

Public Sub GetData()
    Dim i As Integer
    Dim Start, Finish, Duration As Integer

    For i = 1 To End
        Start = GetTickCount

    ' Get new Data         
    ...
    ' Refresh DataArray
        ...
        ' Refresh Graph
        ...

        Finish = GetTickCount
        Duration = Start - Finish
        'Delay: Loop repetition after 1s
        Delay (1000 - Duration)
    Next i
End Sub

@ jccaldz: ?

hi just try this: don't know if it will help never test
try to add an if then else statement...

if Finish = GetickCount then
  Delay (1000 - Duration)
else 
end if

The problem seems to solved...

First I redefined my Delay function using a Timer:

Private Declare Function GetTickCount Lib "kernel32" () As Integer
Private Declare Sub Sleep Lib "kernel32" (ByVal ms As Integer)

Private Sub Timer1_Timer()
    Timer1.Enabled = False
End Sub

Public Sub Delay(ByVal tDelay As Double)
    Timer1.Interval = tDelay
    Timer1.Enabled = True
    Do While Timer1.Enabled
        Sleep 10
        DoEvents
    Loop
End Sub

But that still causes an error message when starting a long-time measurement... :icon_sad:

Then I have put the "Duration" as an absolute value:

...
Duration = Abs(Start - Finish)
Delay (1000 - Duration)
...

Now it works fine even for a long-time running programm. :icon_biggrin:

To stay on the safe side I inserted a condition if the loop process should take longer than one second:

If Duration >= 1000 Then
    Duration = 990
End If

Well everything seems to be okay now - BUT:
I just want to understand...

It seems as if the problem is occuring when the "Duration" gets a negative value (and the Delay function has a value of more than one second).
Why on earth does the delay function have this problem - as the timer interval is set within the program to any value (1000-Duration) ??? :icon_confused:
It shouldn't have problems with timer intervals over 1000ms ?

Thanks for your ideas anyway!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.