Home
VB.NET
Timer Examples
This page was last reviewed on Oct 5, 2023.
Dot Net Perls
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.
First example. 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.
And We use the method TimerElapsed (you can name it whatever you like) to run when the interval is reached.
Detail The 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 Module
TIME: 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 example. This 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).
Shared
Next We call the AddHandler operator to assign the Handler subroutine as the code that is executed every 3 seconds.
Finally We set the Enabled property to True to start the Timer. It begins measuring time elapsed.
Start To start the Timer, the Start() subroutine will have to be called during website startup.
Detail The 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
ASP.NET example. 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.
Then In the 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>
Notes, ASP.NET. 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.
Tip Refresh the page occasionally and you will see the Timer is executing its Handler code.
Detail If you are starting with an empty ASP.NET website, you will want to add a Default.aspx page as well.
Info Default.aspx will trigger the runtime to call the Application_Start and Application_BeginRequest handlers.
A summary. 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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
No updates found for this page.
Home
Changes
© 2007-2024 Sam Allen.