944,131 Members | Top Members by Rank

Ad:
Oct 10th, 2006
0

Problem with timer (urgent)

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bryan110 is offline Offline
7 posts
since Oct 2006
Oct 10th, 2006
0

Re: Problem with timer (urgent)

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
Last edited by jbennet; Oct 10th, 2006 at 6:40 pm.
Moderator
Featured Poster
Reputation Points: 1800
Solved Threads: 575
Moderator
jbennet is offline Offline
16,534 posts
since Apr 2005
Oct 10th, 2006
0
Re: Problem with timer (urgent)
Hi ,

Thanks very much for your help, it helped me.

Bye,

Bryan.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bryan110 is offline Offline
7 posts
since Oct 2006
Oct 10th, 2006
0

Re: Problem with timer (urgent)

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bryan110 is offline Offline
7 posts
since Oct 2006
Oct 20th, 2006
0

Re: Problem with timer (urgent)

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:

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. ...
  2. Timer1.Interval = 1000 * SecondsToTimeout
  3. Timer1.Enabled = True
  4. Do Until Timer1.Enabled = False
  5. DoEvents
  6. Loop
  7. ...
  8.  
  9. Private Sub Timer1_Timer()
  10. Timer1.Enabled = False
  11. 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:

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Public Sub MultiTimeout(SecondsToTimeout)
  2. InstanceNumber = Timer1.UBound + 1
  3. Load Timer1(InstanceNumber)
  4. Timer1(InstanceNumber).Interval = 1000 * SecondsToTimeout
  5. Timer1(InstanceNumber).Enabled = True
  6. Do Until Timer1(InstanceNumber).Enabled = False
  7. DoEvents
  8. Loop
  9. End Sub
  10.  
  11. Private Sub Timer1_Timer(Index As Integer)
  12. Timer1(Index).Enabled = False
  13. End Sub
  14.  
  15. Private Sub Timer2_Timer()
  16. For Counter = Timer1.UBound to 1 Step -1
  17. If Timer1(Counter).Enabled = False Then
  18. Unload Timer1(Counter)
  19. Else
  20. Exit Sub
  21. End If
  22. Next
  23. 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.

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Public Sub VB6Timeout(SecondsToTimeout)
  2. TimeoutStart = Timer
  3. If TimeoutStart < 86400 - SecondsToTimeout Then
  4. Do Until Timer >= TimeoutStart + SecondsToTimeout
  5. DoEvents
  6. Loop
  7. Else
  8. Do While Timer > TimeoutStart
  9. DoEvents
  10. Loop
  11. TimeoutStart = TimeoutStart - 86400
  12. Do Until Timer >= TimeoutStart + SecondsToTimeout
  13. DoEvents
  14. Loop
  15. End If
  16. 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
Reputation Points: 16
Solved Threads: 1
Light Poster
sendoshin is offline Offline
39 posts
since Sep 2006
Oct 20th, 2006
0

Re: Problem with timer (urgent)

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
antonybls is offline Offline
4 posts
since Oct 2006
Oct 20th, 2006
0

Re: Problem with timer (urgent)

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
Reputation Points: 16
Solved Threads: 1
Light Poster
sendoshin is offline Offline
39 posts
since Sep 2006
Oct 25th, 2006
0

Re: Problem with timer (urgent)

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
antonybls is offline Offline
4 posts
since Oct 2006
Oct 25th, 2006
0

Re: Problem with timer (urgent)

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.
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Jul 2nd, 2007
0

Re: Problem with timer (urgent)

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
brijlesh is offline Offline
1 posts
since Jul 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Visual Basic 4 / 5 / 6 Forum Timeline: ADO replace value function
Next Thread in Visual Basic 4 / 5 / 6 Forum Timeline: SSH component or ocx





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC