6

I am trying to use a Timer control in my console application.

Friend WithEvents XTIMER As System.Windows.Forms.Timer

I am setting all its properties. I have set the interval to 15000 ms. But even when I set the Enabled state of the timer control to be true, the tick event is not firing. Can any one help me out please?

1
  • Showing some code would help us diagnose the cause. Sep 29, 2010 at 9:10

4 Answers 4

13
Module Module1

    Sub Main()
        aTimer.AutoReset = True
        aTimer.Interval = 2000 '2 seconds
        AddHandler aTimer.Elapsed, AddressOf tick
        aTimer.Start()
        Console.ReadKey()
    End Sub

    Dim aTimer As New System.Timers.Timer

    Private Sub tick(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs)
        Console.WriteLine("tick")
    End Sub

End Module
3
  • wow thanks. Love that Console.ReadKey() way of keeping the app alive. Oct 23, 2012 at 9:00
  • 1
    I personally prefer to use Threading.Thread.Sleep(Threading.Timeout.Infinite) to keep the app open because it doesn't force the use of a command window. Aug 2, 2013 at 1:38
  • 2
    VB.NET allows you to define the timer with Dim WithEvents aTimer As New System.Timers.Timer, and add Handles aTimer.Tick at the end of the Sub tick definition. You then don't need to explicitly AddHandler.
    – Zev Spitz
    Sep 9, 2013 at 5:51
2

Use the Timer Class

2
  • @@ To ALL,All these works fine in Desktop application but it is not working in onsole Application.The tick event is not firing
    – Rajdeep
    Sep 29, 2010 at 10:20
  • Thanks...This articled helped me.Appreciate your help
    – Rajdeep
    Sep 29, 2010 at 10:25
1

Use System.Timers.Timer instead. Here's a very good comparison of the timer classes.

1
0

Import the System.Windows.Forms reference and use the Timer class.

1
  • 2
    @downvoter: care to comment? This is a perfectly legit option. The fact that you import the System.Windows.Forms reference doesn't make it a WinForms app. If, for example, you wanted to use the Point class, would you roll out your own implementation (as a purist) or use an existing one in System.Drawing? Aug 8, 2013 at 15:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.