Results 1 to 7 of 7

Thread: [RESOLVED] Using random with timer

  1. #1

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Resolved [RESOLVED] Using random with timer

    Ola,

    I want to execute a code between 30 seconds and 5 minutes. Anyone knows how to do this?

    Timer_Tick(...)handles ....
    dim rnd as new random
    for i as integer = 3000 to 30000

    then my mind goes blank...


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Using random with timer

    random interval? try this:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private WithEvents tmr As New Timer
    4.     Dim r As New Random
    5.  
    6.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    7.         tmr.Interval = r.Next(30000, 300001) '30sec to 5min
    8.         tmr.Enabled = True 'start tmr
    9.     End Sub
    10.  
    11.     Private Sub tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmr.Tick
    12.         'your code here
    13.     End Sub
    14.  
    15. End Class

  3. #3

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Re: Using random with timer

    He Paul, got a small issue.
    Private WithEvents tmr As New Timer
    Error 1 - Overload resolution failed because no accessible 'New' accepts this number of arguments.
    Error 2 - 'Interval' is not a member of 'System.Threading.Timer'.

    Edit: Never mind, because I have imported System.Threading, it took over the timer. I changed it to: Public WithEvents tmr As New System.Timers.Timer
    Last edited by Radjesh Klauke; May 21st, 2012 at 02:41 AM.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Using random with timer

    Just add the Timer to your form in the designer and then you can set the Interval in code. Do you want to select a random interval once and then use that value all the time or do you want the interval between Ticks to vary randomly? If it's the former then .paul.'s code is basically all you need:
    vb.net Code:
    1. Public Class Form1
    2.  
    3.     'This is just so we can see what's happening.
    4.     Private watch As New Stopwatch
    5.  
    6.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    7.         Dim minInterval = 500
    8.         Dim maxInterval = 2500
    9.  
    10.         Timer1.Interval = New Random().Next(minInterval, maxInterval + 1)
    11.         Timer1.Start()
    12.  
    13.         'This is just so we can see what's happening.
    14.         watch.Start()
    15.     End Sub
    16.  
    17.     Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    18.         'Do your work here.
    19.  
    20.         'This is just so we can see what's happening.
    21.         Console.WriteLine(watch.Elapsed.ToString())
    22.     End Sub
    23.  
    24. End Class
    I actually interpreted your question to mean that you wanted the interval between Ticks to vary randomly over time. For that you would need a little bit more:
    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Private minInterval As Integer = 500
    4.     Private maxInterval As Integer = 2500
    5.  
    6.     Private rng As New Random
    7.  
    8.     'This is just so we can see what's happening.
    9.     Private watch As New Stopwatch
    10.  
    11.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    12.         SetRandomInterval()
    13.         Timer1.Start()
    14.  
    15.         'This is just so we can see what's happening.
    16.         watch.Start()
    17.     End Sub
    18.  
    19.     Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    20.         'Change the Interval for the next Tick.
    21.         SetRandomInterval()
    22.  
    23.         'Do your work here.
    24.  
    25.         'This is just so we can see what's happening.
    26.         Console.WriteLine(watch.Elapsed.ToString())
    27.     End Sub
    28.  
    29.     Private Sub SetRandomInterval()
    30.         Timer1.Interval = rng.Next(minInterval, maxInterval + 1)
    31.     End Sub
    32.  
    33. End Class
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Re: Using random with timer

    The code of Paul was basically what I needed. But I learned from your code again. Thank you for that.
    Oh and if people are using the code of Paul: It is ...(...)Handles Timer.Elapsed (looked it up in the MSDN)


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Using random with timer

    Quote Originally Posted by Radjesh Klauke View Post
    Oh and if people are using the code of Paul: It is ...(...)Handles Timer.Elapsed (looked it up in the MSDN)
    That would be if you are using a System.Timers.Timer. With the System.Windows.Forms.Timer the event of interest is Tick, not Elapsed. If you add a Timer from the Toolbox to a form in a WinForms app then it will be a Windows.Forms.Timer. You can create either in code. The main difference is their thread-affinity.

    http://msdn.microsoft.com/en-us/magazine/cc164015.aspx

    There's also the DispatcherTimer for WPF now as well.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Re: [RESOLVED] Using random with timer

    Thank you for the link. There are some things that I need to take a closer look at (Parts I don't understand). Some lecture for this evening I guess.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

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