You see it all the time, people saying "Dont use timers, they hog resources". Well i remember when i understood why not to, but never knew how not to. Here i will try and help people that do not know how, to learn how.
Here it goes.
First we are going to start of by creating a new sub routine. This is where the code that you would normally put in a timer is going to be.
Next lets declare some variables in the Form Class. Here you will see why we created the sub routine first.
tCallback is the delegate that points to the method we want the timer to call. Which in our case is the sub we just created: action
Now all we have left to do is call the timer.
tCallback is our delegate that points to the sub action()
nothing is there because we are not passing any information to the sub. This could be used for instance to send ByVal information to action()
1000 is setting the code to start after 1 second
5000 is setting the interval to repeat every 5 seconds.
Now to stop the timer, simply use this code:
tTime.Dispose
And that is the basic run down on an alternative to using timers.
I did not include how to access controls outside of this timer so as not to confuse people before they got a grasp on the basics. However it is quite simple. Once you have a grip on this and still cant figure it out, feel free to reply and i will show you the code. Thank you for reading.
Here is the full code. I have two buttons, Button1 starts it and Button 2 stops it.
Here it goes.
First we are going to start of by creating a new sub routine. This is where the code that you would normally put in a timer is going to be.
Code:
Private sub action()
messagebox.show("it worked")
End Sub
Next lets declare some variables in the Form Class. Here you will see why we created the sub routine first.
Code:
Dim tTime As Threading.Timer
Dim tCallback As Threading.TimerCallback = AddressOf action
tCallback is the delegate that points to the method we want the timer to call. Which in our case is the sub we just created: action
Now all we have left to do is call the timer.
Code:
. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
tTime = New Threading.Timer(tCallback, nothing, 1000, 5000)
End Sub
tCallback is our delegate that points to the sub action()
nothing is there because we are not passing any information to the sub. This could be used for instance to send ByVal information to action()
1000 is setting the code to start after 1 second
5000 is setting the interval to repeat every 5 seconds.
Now to stop the timer, simply use this code:
tTime.Dispose
And that is the basic run down on an alternative to using timers.
I did not include how to access controls outside of this timer so as not to confuse people before they got a grasp on the basics. However it is quite simple. Once you have a grip on this and still cant figure it out, feel free to reply and i will show you the code. Thank you for reading.
Here is the full code. I have two buttons, Button1 starts it and Button 2 stops it.
Code:
Public Class Form1
Dim tTime As Threading.Timer
Dim tCallback As Threading.TimerCallback = AddressOf action
Private Sub action(ByVal x As String)
MessageBox.Show("it worked")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
tTime = New Threading.Timer(tCallback, Nothing, 1000, 5000)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
tTime.Dispose()
End Sub
End Class
I code at http://tech.reboot.pro