Using Timer Class
If you have programmed in VB6 before then probably you are familiar with Timer control. The Timer control triggers a code after certain interval of time. This control is typically used on windows forms. Similar control does exists for Windows Forms as well. What if you need such timer functionality on a thread of your classes (not on forms)? The System.Threading.Timer class can be used in such cases. This class automatically uses Thread Pool for executing your code.
Following namespaces are involved:
Using System.Threading.Timer Class
In order to use Timer class you need to create an instance of it as shown below:
Public Sub StartTimer()
Dim tcb As New TimerCallback(AddressOf Me.TimerMethod)
Dim objTimer As Timer
objTimer = New Timer(tcb, Nothing,
Public Sub TimerMethod(ByVal state As Object)
MsgBox("The Timer invoked this method.")
Here, we created an instance of Timer class and passed a TimerCallBack and interval details to it. The third parameter represents the delay in milliseconds after which the method will be called for the first time. The forth parameter represents the interval in milliseconds that will be elapsed between the consecutive method calls. In our example the timer will start 5 seconds after creation of the instance. Then after 10 seconds it will fire again and again.
is a software consultant, trainer, author, yoga mentor, and spiritual guide having 24+ years of experience in software development, consulting, and training. He conducts instructor-led online training courses
in ASP.NET Core, ASP.NET MVC, and Design Patterns for individuals and small groups. He is a published author and has authored or co-authored books for Apress and Wrox press. Having embraced the Yoga way of life he also teaches Ajapa Yoga
to interested individuals. To know more about him click here
Get article updates : Facebook Twitter LinkedIn