Implements a timer that raises an event at user-defined intervals.
[Visual Basic] Public Class Timer Inherits Component [C#] public class Timer : Component [C++] public __gc class Timer : public Component [JScript] public class Timer extends Component
This class provides methods to set the Interval, Start and Stop the timer.
Namespace: System.WinForms
Assembly: System.WinForms.dll
The following example shows a simple interval timer, which sets off an alarm every 3 seconds. When the alarm occurs, a MessageBox displays a count of the number of times the alarm has activated and asks if the user if the timer should continue running.
[Visual Basic]
' CLASS LEVEL DECLARATIONS 'Declare timer, alarm counter and exit flag. Shared myTimer As WinForms.Timer Shared alarmCounter As Integer Shared exitFlag As Boolean 'Run this method when the timer triggers. Shared Sub MyTimerEventProcessor(ByVal myObj As Object, ByVal myEventArgs As EventArgs) myTimer.Stop() If (MessageBox.Show("Alarm went off. Continue running?" , _ "Count = " & alarmCounter, MessageBox.YesNo) = WinForms.DialogResult.Yes) Then 'Continue running the timer. alarmCounter += 1 myTimer.Enabled = True Else exitFlag = False myTimer.Enabled = False End If End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 'Initialize the alarm counter and exit flag. alarmCounter = 1 exitFlag = False 'Declare an event handler for the timer event. Dim myTimerEventHandler As EventHandler myTimerEventHandler = New EventHandler(AddressOf MyTimerEventProcessor) myTimer.AddOnTimer(myTimerEventHandler) 'Set the timer interval to 3 seconds and start it. myTimer.Interval = 3000 myTimer.Start() 'Run the timer and trigger the event. Do While exitFlag = False Application.DoEvents() Loop End Sub Shared Sub Main() 'Initialize the timer. myTimer = New WinForms.Timer End Sub