|
-
May 20th, 2012, 04:10 PM
#1
Thread Starter
PowerPoster
[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...
-
May 20th, 2012, 04:49 PM
#2
Re: Using random with timer
random interval? try this:
vb Code:
Public Class Form1
Private WithEvents tmr As New Timer
Dim r As New Random
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
tmr.Interval = r.Next(30000, 300001) '30sec to 5min
tmr.Enabled = True 'start tmr
End Sub
Private Sub tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmr.Tick
'your code here
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 21st, 2012, 02:27 AM
#3
Thread Starter
PowerPoster
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.
-
May 21st, 2012, 02:50 AM
#4
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:
Public Class Form1 'This is just so we can see what's happening. Private watch As New Stopwatch Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim minInterval = 500 Dim maxInterval = 2500 Timer1.Interval = New Random().Next(minInterval, maxInterval + 1) Timer1.Start() 'This is just so we can see what's happening. watch.Start() End Sub Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick 'Do your work here. 'This is just so we can see what's happening. Console.WriteLine(watch.Elapsed.ToString()) End Sub 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:
Public Class Form1 Private minInterval As Integer = 500 Private maxInterval As Integer = 2500 Private rng As New Random 'This is just so we can see what's happening. Private watch As New Stopwatch Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load SetRandomInterval() Timer1.Start() 'This is just so we can see what's happening. watch.Start() End Sub Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick 'Change the Interval for the next Tick. SetRandomInterval() 'Do your work here. 'This is just so we can see what's happening. Console.WriteLine(watch.Elapsed.ToString()) End Sub Private Sub SetRandomInterval() Timer1.Interval = rng.Next(minInterval, maxInterval + 1) End Sub End Class
-
May 21st, 2012, 04:25 AM
#5
Thread Starter
PowerPoster
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)
-
May 21st, 2012, 04:41 AM
#6
Re: Using random with timer
 Originally Posted by Radjesh Klauke
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.
-
May 21st, 2012, 06:16 AM
#7
Thread Starter
PowerPoster
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.
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
|