| | |
Problem with timer (urgent)
![]() |
•
•
Join Date: Oct 2006
Posts: 7
Reputation:
Solved Threads: 0
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.
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.
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
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
Last edited by jbennet; Oct 10th, 2006 at 6:40 pm.
If i am helpful, please give me reputation points.
•
•
Join Date: Sep 2006
Posts: 36
Reputation:
Solved Threads: 1
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
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:
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
If you need multiple timeouts, the following slight modification should work as well:
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.
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
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:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
... Timer1.Interval = 1000 * SecondsToTimeout Timer1.Enabled = True Do Until Timer1.Enabled = False DoEvents Loop ... Private Sub Timer1_Timer() Timer1.Enabled = False End Sub
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:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
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
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.
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
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
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
•
•
Join Date: Oct 2006
Posts: 4
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Jul 2007
Posts: 1
Reputation:
Solved Threads: 0
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.
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.
![]() |
Similar Threads
- Problem in Timer (Java)
- timer urgent (ASP.NET)
- urgent..i need help.. (C++)
Other Threads in the Visual Basic 4 / 5 / 6 Forum
- Previous Thread: How to Remove Newline character from a VB String
- Next Thread: SSH component or ocx
| Thread Tools | Search this Thread |
* 6 429 2007 access activex add age application basic beginner birth bmp calculator cd cells.find click client code college component connection connectionproblemusingvb6usingoledb copy creat ctrl+f data database datareport date delete dissertations dissertationthesis dissertationtopic edit error excel excelmacro file filename form hardware header iamthwee image inboxinvb internetfiledownload keypress label listbox listview liveperson login looping machine microsoft movingranges number objectinsert open oracle password prime program prompt range-objects readfile reading record refresh remotesqlserverdatabase report save search sendbyte sites sort sql sql2008 sqlserver subroutine tags textbox time urldownloadtofile vb vb6 vb6.0 vba visual visualbasic visualbasic6 web window windows






