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...
Printable View
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...
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
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
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: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 '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 Classvb.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
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)
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.
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.