Results 1 to 35 of 35

Thread: Timer

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2005
    Posts
    300

    Timer

    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 ??
    Im Learning !!!!

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Timer

    Timer1.Interval = 60000 (for 1 min)
    Timer1.Interval = 900000 (for 15 min)

    Just play around with it
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2005
    Posts
    300

    Re: Timer

    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"
    Im Learning !!!!

  4. #4
    Fanatic Member vbasicgirl's Avatar
    Join Date
    Jan 2004
    Location
    Manchester, UK
    Posts
    1,016

    Re: Timer

    if it doesnt have to be precise with the timer then i would set the interval to 60000(1min) and try this
    VB Code:
    1. Option Explicit
    2. Private counter As Long
    3.  
    4.  
    5. Private Sub Form_Load()
    6.  Randomize
    7.  counter = Int(Rnd * 15) + 1
    8. End Sub
    9.  
    10.  
    11. Private Sub Timer1_Timer()
    12.   counter = counter - 1
    13.    If counter = 0 Then
    14.     'run some code here
    15.     counter = Int(Rnd * 15) + 1
    16.    End If
    17. End Sub

    casey.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2005
    Posts
    300

    Re: Timer

    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.
    Im Learning !!!!

  6. #6
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Timer

    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.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2005
    Posts
    300

    Re: Timer

    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 ??
    Im Learning !!!!

  8. #8
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Timer

    Quote Originally Posted by Ricky1
    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"
    Call this just before your "Next i"

    VB Code:
    1. Sub RandomizeTimer()
    2.     Randomize    
    3.     Timer1.Interval = (rnd+15) +1
    4. End Sub
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  9. #9
    Fanatic Member vbasicgirl's Avatar
    Join Date
    Jan 2004
    Location
    Manchester, UK
    Posts
    1,016

    Re: Timer

    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.

  10. #10
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Timer

    Quote Originally Posted by Ricky1
    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 ??
    Do u need a timer at all??

    to make a random selection in listbox

    VB Code:
    1. List1.ListIndex = (rnd * List1.ListCount - 1) + 1


    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2005
    Posts
    300

    Re: Timer

    yeah its not a random selection i want, it a random time between moving from on "i" to the next "i".
    Im Learning !!!!

  12. #12
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Timer

    Quote Originally Posted by vbasicgirl
    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.
    Oh God!
    I'hd just forgot that that property is integer.
    And I think I have not understood his question clearly either.

    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  13. #13
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Timer

    Quote Originally Posted by Ricky1
    yeah its not a random selection i want, it a random time between moving from on "i" to the next "i".
    If u just want a random delay before "Next i" then u can loop to wait there or call the Sleep API
    VB Code:
    1. Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2005
    Posts
    300

    Re: Timer

    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.
    Im Learning !!!!

  15. #15
    Fanatic Member vbasicgirl's Avatar
    Join Date
    Jan 2004
    Location
    Manchester, UK
    Posts
    1,016

    Re: Timer

    what are you trying to do when the timer fires. maybe there is a better way than a for next loop.

    casey.

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2005
    Posts
    300

    Re: Timer

    Quote Originally Posted by Pradeep1210
    If u just want a random delay before "Next i" then u can loop to wait there or call the Sleep API
    VB Code:
    1. Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    yes i want a randomly delay b4 its move on to the next i. but has to be longer then 30 seconds.
    Im Learning !!!!

  17. #17
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Timer

    Quote Originally Posted by Ricky1
    yes i want a randomly delay b4 its move on to the next i. but has to be longer then 30 seconds.
    Insert this before "Next i"

    VB Code:
    1. Sleep ((rnd* 90) +1 ) * 100   ''some random delay upto 90 sec, etc.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2005
    Posts
    300

    Re: Timer

    would i also have to include you code on post #13 ?? A module ? or just at the top of all the code.
    Im Learning !!!!

  19. #19
    Fanatic Member vbasicgirl's Avatar
    Join Date
    Jan 2004
    Location
    Manchester, UK
    Posts
    1,016

    Re: Timer

    the problem with Sleep is it stops all code running throughout the program.

    casey.

  20. #20
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Timer

    Quote Originally Posted by vbasicgirl
    the problem with Sleep is it stops all code running throughout the program.

    casey.
    I think that's just exactly what he wants..

    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  21. #21
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Timer

    Quote Originally Posted by Ricky1
    would i also have to include you code on post #13 ?? A module ? or just at the top of all the code.
    Declaration on the top of form..
    VB Code:
    1. Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    And the code just before "Next i"
    VB Code:
    1. '
    2. '
    3. '
    4.     Sleep ((rnd* 90) +1 ) * 100   ''some random delay upto 90 sec, etc.
    5. Next i

    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  22. #22
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Timer

    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.

  23. #23
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Timer

    Yes nice if you want loops but what if you don't. And Jacob have you found anything wrong with API timers yet?

  24. #24
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Timer

    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?

  25. #25
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Timer

    Quote Originally Posted by Jacob Roman
    Are those Timer API's more accurate and consistant than regular Timer objects?
    Yep, if you can get them to start

    Have they been going 10 times faster on XP machines?
    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.

    I'll attach a little demo of them if I can get my timers class thingo to work

  26. #26
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Timer

    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.
    Attached Files Attached Files

  27. #27

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2005
    Posts
    300

    Re: Timer

    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
    Im Learning !!!!

  28. #28
    Lively Member MET777's Avatar
    Join Date
    Apr 2005
    Posts
    76

    Re: Timer

    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:
    1. timer1.Interval = Int(rnd * 60000)
    2.  
    3.  
    4. counter = counter + 1
    5.  
    6. If counter = 10 Then
    7.     'do whatever
    8. End If
    --Matt

  29. #29
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Timer

    Quote Originally Posted by MET777
    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:
    1. timer1.Interval = Int(rnd * 60000)
    2.  
    3.  
    4. counter = counter + 1
    5.  
    6. If counter = 10 Then
    7.     'do whatever
    8. End If
    Guess what? That's exactly one of the reasons why API timers are used instead! See my attachment above.

  30. #30
    Lively Member MET777's Avatar
    Join Date
    Apr 2005
    Posts
    76

    Re: Timer

    i know that timers are bad, i was just showing him how he could do it if he needed to use timers
    --Matt

  31. #31
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Timer

    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:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     Timer1.Interval = 500
    5.     Timer1.Enabled = True
    6.    
    7.     Randomize
    8. End Sub
    9.  
    10. Private Sub Timer1_Timer()
    11.     Static PrevTime As Single, TmrInterval As Single
    12.    
    13.     If PrevTime = 0 And TmrInterval = 0 Then
    14.         PrevTime = Timer
    15.         TmrInterval = (840 * Rnd) + 60
    16.     End If
    17.    
    18.     If (PrevTime + TmrInterval) < Timer Then
    19.         DoYourThing
    20.        
    21.         PrevTime = Timer
    22.         TmrInterval = (840 * Rnd) + 60
    23.     End If
    24. End Sub
    25.  
    26. Private Sub DoYourThing()
    27.     ' pick a value from your list here...
    28.    
    29.     Debug.Print Timer
    30. End Sub

  32. #32
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Timer

    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.

  33. #33

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2005
    Posts
    300

    Re: Timer

    Quote Originally Posted by CVMichael
    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:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     Timer1.Interval = 500
    5.     Timer1.Enabled = True
    6.    
    7.     Randomize
    8. End Sub
    9.  
    10. Private Sub Timer1_Timer()
    11.     Static PrevTime As Single, TmrInterval As Single
    12.    
    13.     If PrevTime = 0 And TmrInterval = 0 Then
    14.         PrevTime = Timer
    15.         TmrInterval = (840 * Rnd) + 60
    16.     End If
    17.    
    18.     If (PrevTime + TmrInterval) < Timer Then
    19.         DoYourThing
    20.        
    21.         PrevTime = Timer
    22.         TmrInterval = (840 * Rnd) + 60
    23.     End If
    24. End Sub
    25.  
    26. Private Sub DoYourThing()
    27.     ' pick a value from your list here...
    28.    
    29.     Debug.Print Timer
    30. End Sub
    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 .
    Im Learning !!!!

  34. #34
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Timer

    Quote Originally Posted by Ricky1
    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 .
    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...

  35. #35

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2005
    Posts
    300

    Re: Timer

    ok thanks alot Micheal if i have any problems i will post them here.
    Im Learning !!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width