I need help creating a timer in visual basic(console application) RRS feed

  • Question

  • I'd like to create a timer so that after 60 seconds, a variable from the main sub will be set to true. If anyone could help, that would be very much appreciated.

    *fat dab*

    Monday, October 1, 2018 9:03 AM

All replies

  • Try using code like this, adapt to your needs

    Imports System
    Imports System.Timers
    Public Class Example
       Private Shared aTimer As System.Timers.Timer
       Public Shared Sub Main()
    	  Console.WriteLine(vbLf & "Press the Enter key to exit the application..." & vbLf)
    	  Console.WriteLine("The application started at {0:HH:mm:ss.fff}", Date.Now)
    	  Console.WriteLine("Terminating the application...")
       End Sub
       Private Shared Sub SetTimer()
    		' Create a timer with a two second interval.
    		aTimer = New System.Timers.Timer(2000)
    		' Hook up the Elapsed event for the timer. 
    		AddHandler aTimer.Elapsed, AddressOf OnTimedEvent
    		aTimer.AutoReset = True
    		aTimer.Enabled = True
       End Sub
    	Private Shared Sub OnTimedEvent(ByVal source As Object, ByVal e As ElapsedEventArgs)
    		Console.WriteLine("The Elapsed event was raised at {0:HH:mm:ss.fff}", e.SignalTime)
    	End Sub
    End Class

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Proposed as answer by Alex Li-MSFT Tuesday, October 9, 2018 12:53 AM
    Monday, October 1, 2018 9:13 AM
  • Check this too (without a timer class):

    Dim myVariable = False
    Dim t = New Thread(Sub()
                                  myVariable = True
                               End Sub)
    t.IsBackground = True
    . . .

    • Edited by Viorel_MVP Monday, October 1, 2018 10:13 AM
    Monday, October 1, 2018 10:12 AM
  • Hi,

    Do you solve the problem?

    try the code:

    Dim t As New Timer With {.Interval = 6000, .Enabled = True}
                AddHandler t.Tick, Sub()
                                   End Sub

    Best  Regards,


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Tuesday, October 9, 2018 1:56 AM