|
-
Aug 14th, 2004, 01:00 AM
#1
Thread Starter
Frenzied Member
How can you get a timer to execute randomly between 2-4 seconds?
is there any way to set a timer to execute randomly every 2-4 seconds (intervals every 10th of a second, ie 2.0, 2,1, 2,2 etc) instead of a static value?
-
Aug 14th, 2004, 01:19 AM
#2
use a random statement, ie timeout = int(rnd*2000) + 2000 between 2000 and 4000 milliseconds, 2 - 4 seconds.
make sure to use randomize timer to seed the number generator so that you don't get the same sequece each time.
i also saw this code to wait an interval. you need to pass the value of timeout to the routine.
Code:
Public Sub WaitLoop(timeout As Long, Optional myvar As Boolean)
Dim timenow As Long
timenow = GetTickCount
Do
DoEvents
If myvar = True Then
Exit Do
End If
Loop While (GetTickCount - timenow) < timeout
End Sub
http://www.vbforums.com/search.php?s...ost&sortorder=
Last edited by dglienna; Aug 14th, 2004 at 01:35 AM.
-
Sep 1st, 2004, 04:41 AM
#3
Thread Starter
Frenzied Member
is there any way to do it without using a loop that will execute billions of times and put the CPU usage to 100?
-
Sep 1st, 2004, 04:51 AM
#4
Can you please clarify, as the following is contradictory!
execute randomly every 2-4 seconds (intervals every 10th of a second
Bruce.
-
Sep 1st, 2004, 05:08 AM
#5
I think he means the interval should be 2.0, 2.1, 2.2, ... 3.9, 4.0; rather than 2.425 etc.
To create that interval, use a slightly modifed version of dglienna's code:
timeout = (int(rnd*21)*100) + 2000
(this will give values from 2000ms to 4000ms in steps of 100ms)
One of these would do the job:
a) set the interval to the random amount at the end of the Timer event. due to the way the timer works, this isn't going to be accurate (the interval is the maximum time until it will fire - it may fire almost immediately).
b) use a version of what dglienna posted, but use Sleep in addition to DoEvents.
c) set the timer to fire every 100ms (0.1s), then at the start of the timer event check if the interval time has been reached. eg:
VB Code:
'(in the timer event)
Static Inverval_Time as Long
If Inverval_Time > GetTickCount Then Exit Sub
'(your timer code here)
Inverval_Time = GetTickCount + (int(rnd*21)*100) + 2000
I haven't tested this, but it should do the job.
d) use an API timer (I think using SetTimer).
-
Sep 1st, 2004, 06:15 AM
#6
out of curosity what is the point of the myvar in dglienna's code? - it looks pointless to me cause it will always abort b4 doing nething
-
Sep 1st, 2004, 06:52 AM
#7
Errrr...
You lot have just messed with my head 
VB Code:
Private Sub Timer1_Timer()
Timer1.Interval = Clng(((Rnd * 2) + 2) * 1000)
End Sub
The timer fires randomly betweeb 2 and 4 seconds everytime.
Wopka
Last edited by Wokawidget; Sep 1st, 2004 at 08:05 AM.
-
Sep 1st, 2004, 07:46 AM
#8
Not necesarily true, see point a) in my post above.
Also, you code doesnt allow for x.x seconds.
-
Sep 1st, 2004, 08:06 AM
#9
hAHA...OOOPS. HAD MY BRACKET IN THE WRONG PLACE. eDITTED THE CODE. nOW IT DOES ms.
damn caps 
Woof
-
Sep 1st, 2004, 10:47 AM
#10
Originally posted by si_the_geek
Not necesarily true, see point a) in my post above.
Also, you code doesnt allow for x.x seconds.
the x.x seconds issue aside... .what's wrong with resetting the interval length inside the timer event?
As an additional note. the first thing the event should do is disable the timer, then do what it needs to do, then reset the interval time, and lastly, re-enable the timer.
TG
-
Sep 1st, 2004, 11:23 AM
#11
The problem is that the timer control does always not fire at intervalms after the interval has been set (& the control enabled).
It is possible that the event will fire much earlier than intervalms, I just ran a quick test and didn't get the problem, but I have seen it before (and there have been many threads on this forum about this issue).
The event can also happen much later if the system is too busy to process it, and a timer does not tick quite as often if the application it is in doesn't have the focus.
-
Sep 1st, 2004, 02:08 PM
#12
Fanatic Member
Wow I never seen or had that problem with the timer.. its always been right on time for me. But if the original person felt this was an issue and wanted another way to do it without putting his program in a loop, you could set up a DLL with the loop in and just reference / call the DLL, of course you would need to raise an event when the time is reached and handle the event in the primary program. This would cause the loop to run in its own process and not limit the functionality of the primary program by getting bogged down in a loop.
-
Sep 1st, 2004, 06:01 PM
#13
If you disregard that VB is single threaded and that a Timer event might not be raised because your app is doing some other lengthy calculation or something, this code should work:
VB Code:
Private Sub Timer1_Timer()
Timer1.Enabled = False
Timer1.Interval = (Int(Rnd * 20) + 20) * 100
Timer1.Enabled = True
'...
'the rest of the code to execute in the Timer event
End Sub
Cheers,
-
Sep 1st, 2004, 06:43 PM
#14
the timer doesn;t actually miss getting fired when u;re app is busy - it just doesn't count up when it does - so whenever u;re app is executing code the timer pauses
-
Sep 1st, 2004, 07:34 PM
#15
You can skip the problem like this:
- set timer interval to 500 ms or some other value smaller than you actually need, smaller gives better accuracy but in the other hand you increase processor usage...
VB Code:
Private Sub Timer1_Timer()
Static WaitUntil As Long
If WaitUntil > GetTickCount Then Exit Sub
WaitUntil = GetTickCount + 2000 + Int(Rnd * 2000)
'do stuff here
End Sub
'in loops that take very long
Do
'do some stuff here
Timer1_Timer
Loop
Yeah, bubblegum code, but does the work
-
Sep 1st, 2004, 08:47 PM
#16
Originally posted by i00
the timer doesn;t actually miss getting fired when u;re app is busy - it just doesn't count up when it does - so whenever u;re app is executing code the timer pauses
That is somewhat true, except that it isn't the timer that pauses. Whenever the Timer event should be fired Windows sends a WM_TIMER message to your applications message queue. Since VB isn't free threaded it can only handle one message at the time, or run one procedure at the time. If you would use a DoEvents call VB will pause the execution of the current procedure and take care of the messages currently in the cueue.
On another note; After reading this thread I really can't understand why people started talking about this subject at all. VaxoP wanted to set a Timer to a random number between 2 and 4 seconds, so even it the execution of the timer event might be delayed a few milliseconds (even counting 100 milliseconds) wouldn't make any difference at all.
-
Sep 2nd, 2004, 01:20 AM
#17
i coded the random time code for between 2 and 4 seconds.
THEN i posted code that WAS AN EXAMPLE of how to use the timer. I did not expect anyone to actually *use* the code.
Sorry for the extra variable!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|