Timer 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.
First, this class is VB.NET Class that has some Shared fields. It has a Timer instance and also a List instance. In the Start subroutine, we construct the Timer, specifying its interval as 3000 milliseconds (3 seconds).Shared
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 Class
We 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.
Here, we demonstrate how in the VB.NET language you can use the Timer class shown above in your ASP.NET website. First, you should add the Global.asax file. This provides a handy way to call code at your website startup.
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.
If you are starting with an empty ASP.NET website, you will want to add a Default.aspx page as well. This will trigger the runtime to call the Application_Start and Application_BeginRequest handlers.
We added a Timer to an ASP.NET website. The Timer is an excellent way to maintain the data or caches in your website. And because it is separate from request handling, it will not cause slowdowns for visitors on your website.
Also:As an aside, the Timer can also be used in Windows Forms or other kinds of programs.