Search code examples
vb.nettimer

VB.NET Timers Class


Just a conceptual question. I've spent hours trying to find out more about the Timers Class in VB.NET. I can't find a single tutorial or explanation on it that doesn't seem to involve the VB.NET Forms mode. I'm currently working on Console mode where I'm trying to use it.

I've even tried copying code into my program and running it, but it doesn't work. So here's my question, is the Timers class unique to the Forms mode? If not, can someone please just show me a basic implementation or link me to someplace I can learn.


Solution

  • Well, you could I suppose import windows "forms" and use the windows timer class. That timer class can "easy" be used in code, or you can even drop the control onto a form. (so, just like data adaptors etc., you can drop them on the form, or use code to create an instance of such timers).

    So, for forms, if you do NOT drop the control on the screen you can still easy create an instance of the timer control in code. eg this works:

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    
            Dim MyTimer As New Timer
    
            MyTimer.Interval = 1000     ' one second
            MyTimer.Enabled = True
    
            AddHandler MyTimer.Tick, AddressOf TimerTick
    
    
        End Sub
    
        Sub TimerTick()
    
            Debug.Print("timer tick" & "")
    
        End Sub
    

    So, the above is a example of creating the "instance" of the timer class, and then having it call/run the one routine every second. So, above is a example for win-forms.

    However, because you writing/using a console application? Then the above code would not make much sense to try and use since you would be creating a timer "control" object based on the win-forms timer object. To be fair, you likely could get this to work on a console application - (just like you can import windows forms, and launch + run + use winforms in a console application.

    However, for a pure console application?

    There is the .net "timer" class, and it works "similar" to the winforms one, but you have to initialize it somewhat different. And it is not dependent on the typical "timer" object we use for winforms applications.

    So, here is a working test stub, and in the "main" it simply creates a instance of the timer object, and every 1 second calls the routine of your choice.

    Of course, main will "exit" right away, and we would thus not see the code run.

    So, I just just put in a console.ReadLine as the last line on main to "wait" for you to tap the enter key (to exit).

    So, this will work:

    Imports System.Threading
    
    Module Module1
        Sub Main()
    
            Dim o As New AutoResetEvent(False)
            Dim MyTimer As New Timer(AddressOf TimerTick, o, 0, 1000)
    
            ' wait for user input
            Console.ReadLine()
    
        End Sub
    
        Sub TimerTick(o As Object)
    
            Console.WriteLine("timer tick code fire - ")
    
        End Sub
    
    End Module
    

    As noted, it will fire/call the 2nd routine every second. Just tap enter key to exit.

    So the timer class for winforms is "somewhat" different. I suspect ultimately both timer class use/work from the same base .net class, but they are wired up a little different since for a console applications you not working with forms.