.NET Array Dictionary List String Sub ArrayList Cast Class Console Dates DataTable DateTime Enum File For Format If IndexOf Lambda LINQ Nothing Parse Process Property Regex Replace Select Sort Split StringBuilder SubstringTimer lets you call a subroutine every several seconds. You must construct the Timer instance and then add handlers to it. Using the ElapsedEventHandler, you can specify a subroutine to perform the maintenance or updating code.
Next: We call the AddHandler operator to assign the Handler subroutine as the code that is executed every three seconds.
Finally: We set the Enabled property to True to start the Timer. It begins measuring time elapsed.
Class that uses Timer type: VB.NET 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 ClassWe can use this code in an ASP.NET website. To start the Timer, the Start() subroutine shown above will have to be called during website startup. The GetOutput() Function will show the DateTimes that were collected by the Timer.
Note: In the Application_Start handler, we simply call the TimerTest.Start subroutine that was declared in the class.
Then: In the Application_BeginRequest subroutine, we simply output the text of the List collected by the Timer.
Example Global.asax: VB.NET <%@ 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 three seconds. Refresh the page occasionally and you will see the Timer is executing its Handler code.
Also: As an aside, the Timer can also be used in Windows Forms or other kinds of programs.