Hi,

I would like to use a timer with vb6 but i am not allowed to create an object timer with CreateObject.
The main problem is i need to make a delay until the connection is successful. I tried sleep function but it seems it stopping everything.
Please If you have any idea , it 's urgent.

Thanks,

bryan.

Recommended Answers

All 9 Replies

drag and drop the timer onto your form using the timer control from the tolbox (it will be invisible when you run the program) - remember to set its "enabled" property to true and set the "interval" property which is how many times it "ticks" to 1000 for one second and so on

Double click the timer control to add tick event code, in the same eway as you would do for any control e.g command button

Hi ,

Thanks very much for your help, it helped me.

Bye,

Bryan.

Hi,

I need to ask about how to add the ocx , because
i am getting runtime error 429 when i try to use
createObject.

Thanks

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

Till the connection get success or failure, the application will look like hang, then why do u want to put sleep or doEvents functions. instead, you can show the progress in progress bar untill the successfull connection.

Cheers,
bls.

DoEvents allows the system to continue doing what it's doing as it connects. Without DoEvents, status indicators (such as Progress Bars) are not updated, and their code is not executed.

This is why one would want DoEvents in a situation such as this one. Cheers!

- Sen

Hi Sen,

I do agree the usage of DoEvents. But, my doubt is...

then why do we need Timer events in this case?? Timer event automatically executes the code in a specified interval, call the progress bar codes within this timer event, that is enough to display the progress. DoEvents will not help you in this case.

correct me if i am wrong.

- bls.

Doevents will allow windows to do things, such as repaint the progress bar. The timer control makes things fire at a given interval, true, and maybe the thunderdll's will get around to it between interval events, but doevents a good way to make those events happen.

hi,
Please use form_paint event to solve this problem...
bcoz, form_paint event work as a timer....
now, see example below....

dim blTemp as boolean
blTemp=false

you want some delay to do any work....
before start of work you set blTemp as false(which is default).

and when your work is completed then set blTemp=true..

now,
private sub form_paint()
if bltemp=false then
pur you code or call that function.
else
exit sub
endif
end sub

Note: My english is so poor, and you may not understand what i want to explain you....
so, if you understand and sort out your problem then please reply me....
else please send either your code or explain in detail what you want to do...
then i must solve your problem in VB....

thanks.

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.