Ok i need a random timer.
I need it to move to "next i" on a random timer. Between 1min and 15 minutes. How would i achieve this ??
Printable View
Ok i need a random timer.
I need it to move to "next i" on a random timer. Between 1min and 15 minutes. How would i achieve this ??
Timer1.Interval = 60000 (for 1 min)
Timer1.Interval = 900000 (for 15 min)
Just play around with it :)
yeah, but how can i make it random choose an interval between 60000 and 900000 ??? I need it a different for everytime it moves to "next i"
if it doesnt have to be precise with the timer then i would set the interval to 60000(1min) and try this
VB Code:
Option Explicit Private counter As Long Private Sub Form_Load() Randomize counter = Int(Rnd * 15) + 1 End Sub Private Sub Timer1_Timer() counter = counter - 1 If counter = 0 Then 'run some code here counter = Int(Rnd * 15) + 1 End If End Sub
casey.
Is there gona make them all very similar Casey ?? Because i was hoping to make it very random. Thats y i have such a large width. 1-15.
The VB TImer doesnt go up to 15 minutes because the Interval is a short integer (16 bits only). You need an API timer for that long.
hmmm ok, is there a way i can make a list and get it to randomly pick a value out, so if i add like 30 different timers to the list and then VB with randomly choose one ??
Call this just before your "Next i"Quote:
Originally Posted by Ricky1
VB Code:
Sub RandomizeTimer() Randomize Timer1.Interval = (rnd+15) +1 End Sub
if you run that code, it does fire the timer every minute but will only run your code randomly 1 - 15 minutes. as penagate as pointed out, the timer wont go further than just over a minute.
casey.
Do u need a timer at all?? :rolleyes:Quote:
Originally Posted by Ricky1
to make a random selection in listbox
VB Code:
List1.ListIndex = (rnd * List1.ListCount - 1) + 1
Pradeep :)
yeah its not a random selection i want, it a random time between moving from on "i" to the next "i".
Oh God!Quote:
Originally Posted by vbasicgirl
I'hd just forgot that that property is integer.
And I think I have not understood his question clearly either.
Pradeep :blush:
If u just want a random delay before "Next i" then u can loop to wait there or call the Sleep APIQuote:
Originally Posted by Ricky1
VB Code:
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Ok is this an easier way of acheiving it because im really confussed.
Could i not make a list of like 30 different timer values ? and then get VB to randomly selected one to equal "timer.interval" ???
I have never used timers before, and not got a vaste knowledge of VB, apprecaiting the help.
what are you trying to do when the timer fires. maybe there is a better way than a for next loop.
casey.
yes i want a randomly delay b4 its move on to the next i. but has to be longer then 30 seconds.Quote:
Originally Posted by Pradeep1210
Insert this before "Next i"Quote:
Originally Posted by Ricky1
VB Code:
Sleep ((rnd* 90) +1 ) * 100 ''some random delay upto 90 sec, etc.
would i also have to include you code on post #13 ?? A module ? or just at the top of all the code.
the problem with Sleep is it stops all code running throughout the program.
casey.
I think that's just exactly what he wants..Quote:
Originally Posted by vbasicgirl
Pradeep :)
Declaration on the top of form..Quote:
Originally Posted by Ricky1
And the code just before "Next i"VB Code:
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
VB Code:
' ' ' Sleep ((rnd* 90) +1 ) * 100 ''some random delay upto 90 sec, etc. Next i
Pradeep :)
Ok here's the scoop, you should never use the Sleep API to create a delay in a loop because it causes your program to halt a certain number of milliseconds, causing any code executions and events in your app to halt as well. DoEvents wouldn't even allow events to be executed either when the Sleep API is called for a long period of time. Secondly, you should never use multiple timers, or even one timer. One timer alone is slow, inaccurate, and inconsistant, and gets worse when using more timers. On top of that, when your app is used on XP and you are using a timer, timers are 10 times faster than normal, making it really inaccurate and inconsistant. The only thing that is good about a timer is the fact that it maintains low CPU usage. I recommend managed Do Loops, or While Wend loops with using either the GetTickCount API or the QueryPerformanceFrequency & QueryPerformanceCounter API's to manage it. It's higher CPU usage, but the accuracy is worth it. ;)
Yes nice if you want loops but what if you don't. And Jacob have you found anything wrong with API timers yet? ;)
I dunno, I haven't tried since managed loops came into my life after I found out the hard way back when I was addicted to Timers that Timers are really slow and inaccurate. Are those Timer API's more accurate and consistant than regular Timer objects? Have they been going 10 times faster on XP machines?
Yep, if you can get them to start :pQuote:
Originally Posted by Jacob Roman
I dunno, I've never actually noticed any timers going faster on XP machines. But then, I didn't use them much in any older versions.Quote:
Have they been going 10 times faster on XP machines?
I'll attach a little demo of them if I can get my timers class thingo to work :D
Sorry took so long, I couldn't get the collection thing to work (of all things)
Small demo showing use of one timer, with support for multiple timers.
OK im really confussed, i just want to make the timer change value before it moves to the "next i" .
So
Code:'Do what i did
'change the timer interval to a random number
'next i
end sub
one thing u can do.. even tho itd be really inefficeint... would be to use if statements in your timer for example:
if you want such a long delay that the timer will not allow that high an interval ( it only goes up to 65000 ish) do this.
VB Code:
timer1.Interval = Int(rnd * 60000) counter = counter + 1 If counter = 10 Then 'do whatever End If
Guess what? That's exactly one of the reasons why API timers are used instead! See my attachment above.Quote:
Originally Posted by MET777
i know that timers are bad, i was just showing him how he could do it if he needed to use timers
You guys are weird, why you are arguing ?
Ask yourselves this question, and think about it:
If you need a random interval, why does the timer have to be precise ? ammm... random interval !!
And plus that his interval is between 1 -15 minutes, even if the timer is inacurate to 1 second, why does it matter when the interval is RANDOM ?
The timer is inacurate for couple of milliseconds (10-20 milliseconds), so why worry ?
Here's the code I sugest to use:
(Put a timer on the form)
VB Code:
Option Explicit Private Sub Form_Load() Timer1.Interval = 500 Timer1.Enabled = True Randomize End Sub Private Sub Timer1_Timer() Static PrevTime As Single, TmrInterval As Single If PrevTime = 0 And TmrInterval = 0 Then PrevTime = Timer TmrInterval = (840 * Rnd) + 60 End If If (PrevTime + TmrInterval) < Timer Then DoYourThing PrevTime = Timer TmrInterval = (840 * Rnd) + 60 End If End Sub Private Sub DoYourThing() ' pick a value from your list here... Debug.Print Timer End Sub
I wasn't arguing, sorry if it came across that way. I was pointing out that since the instrinsic Timer control's Interval property is a short Integer you can't actually specify an interval of 15 minutes, hence the use of an API timer.
cheers this looks useful, DoYourThing is where put the code i want the program to do ? which is do webbrowser navigate to blah & list1.list(i) next i .Quote:
Originally Posted by CVMichael
Yup... don't worry about previous posts, you don't need precision for what you have to do... just put code in the "DoYourThing" sub...Quote:
Originally Posted by Ricky1
ok thanks alot Micheal if i have any problems i will post them here.