Why are you using CreateObject at all? Do you have multiple connections to set a timeout for? Unless you need more than one timer, you can dispense with CreateObject altogether and just use the Timer control directly.
Even if you do require mulitple instances of the Timer, CreateObject won't work. Here's what you'll need to do to get everything to work correctly. The first step is to go to the Timer control's properties (during design-time, not in code) and set its Index to 0. This turns the control into a control array, albeit one with only a single element. Then, when you create your multiple instances, use something like
Load Timer1(NewTimerNum). NewTimerNum is a variable that keeps track of which Timer you're using for each connection, and Timer1 is your Timer control.
I'm not sure exactly how you're using this, but perhaps this example will be of some use to you. This is how I would impliment a single timeout using a Timer control:
...
Timer1.Interval = 1000 * SecondsToTimeout
Timer1.Enabled = True
Do Until Timer1.Enabled = False
DoEvents
Loop
...
Private Sub Timer1_Timer()
Timer1.Enabled = False
End Sub
The above code sets the timeout, activates the timer control, then waits for the timeout to expire (thus deactivating the timer control), processing other system events while it waits. The "..." before and after the top portion of the code is where the rest of your program goes. You could easily take the code between the "..."s and put it in your program anywhere you want a timeout to occur. Or, you could replace the "..."s with a
Public Sub SingleTimeout(SecondsToTimeout) and
End Sub, respectively, and call the SingleTimeout instead.
If you need multiple timeouts, the following slight modification should work as well:
Public Sub MultiTimeout(SecondsToTimeout)
InstanceNumber = Timer1.UBound + 1
Load Timer1(InstanceNumber)
Timer1(InstanceNumber).Interval = 1000 * SecondsToTimeout
Timer1(InstanceNumber).Enabled = True
Do Until Timer1(InstanceNumber).Enabled = False
DoEvents
Loop
End Sub
Private Sub Timer1_Timer(Index As Integer)
Timer1(Index).Enabled = False
End Sub
Private Sub Timer2_Timer()
For Counter = Timer1.UBound to 1 Step -1
If Timer1(Counter).Enabled = False Then
Unload Timer1(Counter)
Else
Exit Sub
End If
Next
End Sub
This requires a second Timer (Timer2) to unload the unused Timers every Timer2.Interval seconds so you can conserve memory, but it allows you to use multiple timeouts independent of each other. Oh, and don't forget to set Timer1.Index to 0 in design mode before you try to use this!
There is one other method that does not require any Timer controls at all, and that is to use the Timer function provided as a part of VB6 itself.
Public Sub VB6Timeout(SecondsToTimeout)
TimeoutStart = Timer
If TimeoutStart < 86400 - SecondsToTimeout Then
Do Until Timer >= TimeoutStart + SecondsToTimeout
DoEvents
Loop
Else
Do While Timer > TimeoutStart
DoEvents
Loop
TimeoutStart = TimeoutStart - 86400
Do Until Timer >= TimeoutStart + SecondsToTimeout
DoEvents
Loop
End If
End Sub
It looks a bit dirtier, but actually uses less memory, which makes your program run faster.
You might be wondering what all the extra fuss is about with the If statement. Since the Timer function returns the number of seconds since midnight, you have to be careful about timeouts which extend beyond midnight. If you don't have some mechanism in place to handle the fact that Timer = 86399 at 23:59:59 (11:59:59 PM), and then 0 at 00:00:00 (midnight), your timeout will not work correctly. To illustrate, let's say you start the timeout at 15 seconds to midnight, and it has a 30 second timeout. If you try to check for 86415 (StartTime would be 86385, so StartTime + SecondsToTimeout would be 86415), you will be sitting there forever, because Timer never actually reaches 86400 (the number of seconds in a day). So you have to check for 15 instead, which is 00:00:15 (12:00:15 AM). The problem with this is, since we have to check using >= just in case the code isn't executed EXACTLY at 00:00:15 (which is normally the case), we need to make sure that Timer has reset to 0 (midnight) before we start testing if the timeout has passed or not. This is done by waiting until Timer is no longer a number larger than StartTimeout. Then, to calculate how far into the new day we should wait, we turn StartTimeout into a negative number (the timeout was started Abs(StartTimeout) seconds before today started) before adding SecondsToTimeout and performing the check against Timer. All of this is done in the above code.
This last example (VB6Timeout()) combines two key advantages: it can support multiple timeouts from one Private Sub, much like MultiTimeout() above; and (unlike MultiTimeout) it doesn't require any extra controls or objects, which conserves memory and boosts speed. All things being the same, I recommend using VB6Timeout().
I hope this is helpful! Go well, and may the Winds favor you in your journey!
- Sendoshin