I'm new to programming in general, so please bear with me.
This VB6 program I'm using (I'm new at a job) uses an older, but updated Borland C++ DLL (which I'm also updating) to get a part list from.
Everything works fine in debug mode in the VB6 IDE, but when I create an actual executable file and try it, it crashes. The "fix" for this was already implemented years before I got here, they added a bunch of WaitFor function calls to "slow VB6 down so it can finish reading from the DLL before moving on."
The weird thing is, I'm updating the VB6 code by placing some of the hard-coded if-checks for specific parts and putting it into the DLL, because they couldn't get the Borland C++ DLL updated previously. The side-effect of course is that the VB6 code is running faster.
I've narrowed the problem down to simply timing. There is one section where if I just add a simple MsgBox, it won't crash, but WaitFor, even if set to 100000... won't work.
So my question is, can I prevent the VB6 code from moving on without finishing reading from the DLL without arbitrarily adding WaitFors, etc. This seems like a poor practice and I'm going to be at this company for awhile and don't want to continue this trend.
Thank you!
Update:
Doing this in place of the MsgBox seems to make it work as well:
Dim x as Long
Dim y as Long
For x= 1 to 500
y = y + 1
Next
Simply adding that nonsense fixes the issue.
The WaitFor function:
On Error Resume Next
Static dLastDoevents As Double
Dim SleepyTime As Integer: SleepyTime = 20 'set "Sleep peroid" in milliseconds (this is also the resolution of the doevents rate)
Dim DoEventsRate As Integer: DoEventsRate = 200 'set "DoEvents rate" in milliseconds
Dim iSleepTimes As Integer: iSleepTimes = Int(MilliSeconds / SleepyTime)
Dim msLeftOver As Integer: msLeftOver = (MilliSeconds - (iSleepTimes * SleepyTime))
Dim dNow As Double
Dim i As Integer
If MilliSeconds < 0 Then MilliSeconds = 0
dNow = Timer
If dNow > (dLastDoevents + (DoEventsRate / 1000)) Or dLastDoevents = 0 Or dLastDoevents > dNow Then
DoEvents 'DoEvents if it's been long enough according to the DoeventsRate
dLastDoevents = Timer
End If
If iSleepTimes > 0 Then
For i = 1 To iSleepTimes
Sleep (SleepyTime) 'Sleep at intervals of SleepyTime
dNow = Timer
If dNow > (dLastDoevents + (DoEventsRate / 1000)) Or dLastDoevents = 0 Or dLastDoevents > dNow Then
DoEvents 'DoEvents if it's been long enough according to the DoeventsRate
dLastDoevents = Timer
End If
Next i
End If
If msLeftOver > 0 Then
Sleep (msLeftOver) 'Sleep for the remainder of milliseconds if needed (ie WaitFor 5)
dNow = Timer
If dNow > (dLastDoevents + (DoEventsRate / 1000)) Or dLastDoevents = 0 Or dLastDoevents > dNow Then
DoEvents 'DoEvents if it's been long enough according to the DoeventsRate
dLastDoevents = Timer
End If
End If