Problem with timer (urgent)

Reply

Join Date: Oct 2006
Posts: 7
Reputation: bryan110 is an unknown quantity at this point 
Solved Threads: 0
bryan110 bryan110 is offline Offline
Newbie Poster

Problem with timer (urgent)

 
0
  #1
Oct 10th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 16,142
Reputation: jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all 
Solved Threads: 531
Moderator
Featured Poster
jbennet's Avatar
jbennet jbennet is offline Offline
Moderator

Re: Problem with timer (urgent)

 
0
  #2
Oct 10th, 2006
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.
If i am helpful, please give me reputation points.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 7
Reputation: bryan110 is an unknown quantity at this point 
Solved Threads: 0
bryan110 bryan110 is offline Offline
Newbie Poster
 
0
  #3
Oct 10th, 2006
Hi ,

Thanks very much for your help, it helped me.

Bye,

Bryan.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 7
Reputation: bryan110 is an unknown quantity at this point 
Solved Threads: 0
bryan110 bryan110 is offline Offline
Newbie Poster

Re: Problem with timer (urgent)

 
0
  #4
Oct 10th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 36
Reputation: sendoshin is an unknown quantity at this point 
Solved Threads: 1
sendoshin sendoshin is offline Offline
Light Poster

Re: Problem with timer (urgent)

 
0
  #5
Oct 20th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 4
Reputation: antonybls is an unknown quantity at this point 
Solved Threads: 0
antonybls antonybls is offline Offline
Newbie Poster

Re: Problem with timer (urgent)

 
0
  #6
Oct 20th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 36
Reputation: sendoshin is an unknown quantity at this point 
Solved Threads: 1
sendoshin sendoshin is offline Offline
Light Poster

Re: Problem with timer (urgent)

 
0
  #7
Oct 20th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 4
Reputation: antonybls is an unknown quantity at this point 
Solved Threads: 0
antonybls antonybls is offline Offline
Newbie Poster

Re: Problem with timer (urgent)

 
0
  #8
Oct 25th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Problem with timer (urgent)

 
0
  #9
Oct 25th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 1
Reputation: brijlesh is an unknown quantity at this point 
Solved Threads: 0
brijlesh brijlesh is offline Offline
Newbie Poster

Re: Problem with timer (urgent)

 
0
  #10
Jul 2nd, 2007
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Visual Basic 4 / 5 / 6 Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC