Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tutorial]Using threading instead of timer[/Tutorial]
#1
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
Reply
#2
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?
My software company: Porosis Software
Games: Terrantula
Apps: Mathanasis, ColorGrabber
Reply
#3
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [HELP]Tutorial/Learning System[HELP] norBATman 2 1,309 09-08-2012, 10:17 PM
Last Post: AceInfinity
  [TuToRiAl] How to make Offline (No Internet) Captcha (Human Confirmation) [TuToRiAl] Resistance 24 12,535 08-30-2012, 10:14 PM
Last Post: Resistance
  [Visual Basic] FTP Uploader [Tutorial] Coding Support 6 3,011 08-12-2012, 12:36 AM
Last Post: Kenneth
  [RELEASE]SF-TutPad "Tutorial Editor" [RELEASE] Digital-Punk 28 10,219 08-11-2012, 11:25 PM
Last Post: Kenneth
  Emailer Tutorial dunlop03 12 2,849 06-24-2012, 04:33 AM
Last Post: kpn37z

Forum Jump:


Users browsing this thread: 1 Guest(s)