Timer
This class
lets us call a subroutine every several seconds. We must construct a Timer
instance and then add handlers to it.
Using the ElapsedEventHandler
, we can specify a subroutine to perform maintenance or update data. We call Start, and use ElapsedEventArgs
and SignalTime
.
Here we create a new Timer
with its constructor—we specify an interval of 200 milliseconds. We use the AddHandler
operator to set the Elapsed event.
TimerElapsed
(you can name it whatever you like) to run when the interval is reached.ElapsedEventArgs
gives us access to SignalTime
, which is a DateTime
. It is the time the Timer
was fired.Imports System.Timers Module Module1 Sub Main() Dim timer As Timer = New Timer(200) AddHandler timer.Elapsed, New ElapsedEventHandler(AddressOf TimerElapsed) timer.Start() ' Wait and run the timer. Console.Read() End Sub Sub TimerElapsed(ByVal sender As Object, ByVal e As ElapsedEventArgs) ' Write the SignalTime. Dim time As DateTime = e.SignalTime Console.WriteLine("TIME: " + time) End Sub End ModuleTIME: 4/3/2019 2:49:43 PM TIME: 4/3/2019 2:49:43 PM TIME: 4/3/2019 2:49:44 PM TIME: 4/3/2019 2:49:44 PM TIME: 4/3/2019 2:49:44 PM TIME: 4/3/2019 2:49:44 PM TIME: 4/3/2019 2:49:44 PM TIME: 4/3/2019 2:49:45 PM
Class
exampleThis class
has a Timer
instance and a List
instance. In the Start subroutine, we construct the Timer
, specifying its interval as 3000 milliseconds (3 seconds).
AddHandler
operator to assign the Handler subroutine as the code that is executed every 3 seconds.Enabled
property to True to start the Timer
. It begins measuring time elapsed.Timer
, the Start()
subroutine will have to be called during website startup.GetOutput()
Function will show the DateTimes
that were collected by the Timer
.Imports Microsoft.VisualBasic Imports System.Timers Public Class TimerTest Shared _timer As Timer Shared _list As List(Of String) = New List(Of String) ''' <summary> ''' Start the timer. ''' </summary> Shared Sub Start() _timer = New Timer(3000) AddHandler _timer.Elapsed, New ElapsedEventHandler(AddressOf Handler) _timer.Enabled = True End Sub ''' <summary> ''' Get timer output. ''' </summary> Shared Function GetOutput() As String Return String.Join("<br>", _list) End Function ''' <summary> ''' Timer event handler. ''' </summary> Shared Sub Handler(ByVal sender As Object, ByVal e As ElapsedEventArgs) _list.Add(DateTime.Now.ToString()) End Sub End Class
First, we should add the Global.asax file. In the Application_Start
handler, we call the TimerTest.Start
subroutine that was declared in the class
.
Application_BeginRequest
subroutine, we output the text of the List
collected by the Timer
.<%@ Application Language="VB" %> <script runat="server"> Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs) TimerTest.Start() End Sub Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs) Response.Write(TimerTest.GetOutput()) End Sub </script>
When you first visit the website, the Timer
is started. From this point on, the Timer
will add a String
to the List
every 3 seconds.
Timer
is executing its Handler code.Default.aspx
page as well.Default.aspx
will trigger the runtime to call the Application_Start
and Application_BeginRequest
handlers.Timer
is an excellent way to maintain the data or caches in a website. And because it is separate from request handling, it will not cause slowdowns for visitors.