Support Forums

Full Version: [Tutorial]Using threading instead of timer[/Tutorial]
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.

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
This is a timer, too right? Just a different type of a timer. Why would normal timers "hog resources" and this one won't? Can you explain it?
(11-11-2010, 08:30 AM)thanasis2028 Wrote: [ -> ]This is a timer, too right? Just a different type of a timer. Why would normal timers "hog resources" and this one won't? Can you explain it?

I posted this on HF as well, and got told that it didnt hog resources. But on my quest to find out the truth i came up blank. So that still may or may not be true, i was just baseing that off of what Reputable coders had said. I did however find that the Form timer, uses the UI thread and can cause problems with freezing the form with bigger loads. However the method i use performs the actions on its own thread. I have asked Coders that actually know what they are talking about to shed some light on my post. If i find something out i will post it here, or you can follow it at HF.

http://www.hackforums.net/showthread.php...pid7623224