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?
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?
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=
is there any way to do it without using a loop that will execute billions of times and put the CPU usage to 100?
Can you please clarify, as the following is contradictory!
Quote:
execute randomly every 2-4 seconds (intervals every 10th of a second
Bruce.
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:
I haven't tested this, but it should do the job.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
d) use an API timer (I think using SetTimer).
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
Errrr...:confused:
You lot have just messed with my head :(
The timer fires randomly betweeb 2 and 4 seconds everytime.VB Code:
Private Sub Timer1_Timer() Timer1.Interval = Clng(((Rnd * 2) + 2) * 1000) End Sub
Wopka
Not necesarily true, see point a) in my post above.
Also, you code doesnt allow for x.x seconds. ;)
hAHA...OOOPS. HAD MY BRACKET IN THE WRONG PLACE. eDITTED THE CODE. nOW IT DOES ms.
damn caps :(
Woof
the x.x seconds issue aside... .what's wrong with resetting the interval length inside the timer event?Quote:
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. ;)
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
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.
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.
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:Cheers,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
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
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 :)
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.Quote:
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
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.
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! :rolleyes:
:D