In visual basic, the Timer component is useful to raise an event repeatedly in our application based on the specified interval of time.
The timer component is available with System.Timers
namespace. So, to work with the timer component in our application to raise an event after a set of interval we need to import the System.Timers namespace.
Following is the example of defining the timer object that raises an elapsed event after a set of interval in visual basic.
' Create timer
Dim timer As Timer = New Timer()
timer.Interval = 2000
AddHandler timer.Elapsed, AddressOf TimerEvent
timer.AutoReset = True
timer.Enabled = True
If you observe the above code, we created a timer object by using the Timer
class. Here, the Interval
property is used to specify the time interval to raise an Elapsed event (TimerEvent) and the Enabled
property is used to specify whether the timer object should raise an Elapsed event or not.
The AutoReset
property is used to configure the timer object to raise an elapsed event repeatedly at the specified interval of time defined by the Interval
property.
In case, if we want to raise the Elapsed event only once after the specified interval has elapsed, then we need to set the AutoReset
property to false
.
The following table lists some of the most commonly used properties of Timer class in visual basic.
Property | Description |
---|---|
AutoReset | It is useful to get or set whether the Timer should raise the Elapsed event only once (false) or repeatedly (true). |
CanRaiseEvents | It will return a value that indicates whether the component can raise an event or not. |
DesignMode | It will return a value that indicates whether the component is in design mode or not. |
Enabled | It is useful to get or set a value to indicate whether the Timer should raise the Elapsed event or not. |
Interval | It is useful to get or set the interval in milliseconds at which to raise the Elapsed event. |
The following table lists some of the most commonly used methods of Timer class in visual basic.
Method | Description |
---|---|
Start() | It is useful to start raise the Elapsed event by setting Enabled to true. |
Stop() | It will stop raising the Elapsed event by setting Enabled to false. |
Close() | It is useful to release the resources that are used by the Timer. |
Dispose() | It is useful to release all the resources used by the Component. |
MemberwiseClone() | It is useful to create a shallow copy of the current object. |
GetType() | It is useful to get the type of current instance. |
Following is the example of using Timer
class in visual basic to execute the Elapsed events repeatedly at a specified interval of time.
Imports System.Timers
ModuleModule1
Sub Main(ByVal args As String())
Console.WriteLine("Present Enter Key to Exit the Application")
' Create timer
Dim timer As Timer = New Timer()
timer.Interval = 2000
AddHandler timer.Elapsed, AddressOf TimerEvent
timer.AutoReset = True
timer.Enabled = True
Console.ReadLine()
End Sub
' Elapsed Event
Private Sub TimerEvent(ByVal source As Object, ByVal e As ElapsedEventArgs)
Console.WriteLine("Event Raised at {0:HH:mm:ss.fff}", e.SignalTime)
End Sub
End Module
If you observe the above example, we imported System.Timers
namespace to create a timer object by using Timer
class. Here, we used Interval
property to specify the time interval to raise an Elapsed event (TimerEvent) and the Enabled
property to specify whether the timer object should raise an Elapsed event or not.
When we execute the above visual basic program, we will get the result as shown below.
Present Enter Key to Exit the Application
Event Raised at 07:48:49.274
Event Raised at 07:48:51.275
Event Raised at 07:48:53.275
Event Raised at 07:48:55.275
If you observe the result, for every 2 seconds the Elapsed event (TimerEvent) is raising and executing the elapsed event code.
This is how we can use the timer object to raise an event repeatedly at the specified interval of time based on our requirements.