Adds an event handler for the OnTimer event.
[Visual Basic] Public Sub AddOnTimer( _ ByVal handler As EventHandler _ ) [C#] public void AddOnTimer( EventHandler handler ); [C++] public: void AddOnTimer( EventHandler* handler ); [JScript] public function AddOnTimer( handler : EventHandler );
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. This example does not contain a form, only a code module.
[Visual Basic]
' MODULE LEVEL DECLARATIONS ' Declare timer Dim Timer1 As App.Timer ' Declare a counter for the number of alarms. Dim alarmCounter1 As Integer ' Declare a flag to set when the timer is turned off, so the application will exit. Dim exitFlag1 As Boolean Sub Timer1EventProcessor(ByVal blah1 As Object, ByVal EventArgs1 As EventArgs) ' Method to run whenever the timer goes off. Timer1.Stop If (MessageBox.Show("Alarm went off. Continue running?", "Timer1 Count = " & alarmCounter1, Messagebox.YesNo) = DialogResult.Yes) Then ' Continue running the timer. alarmCounter1 += 1 ' Another way to start the timer running. Timer1.Enabled = True ' Stop the timer and exit the application. Else : exitFlag1 = True End If End Sub Shared Sub Main() 'The main entry point for the application. ' Declare an event handler for the timer event. Dim Timer1EventHandler As EventHandler ' Initialize the exit flag. exitFlag1 = False ' Initialize the alarm counter. alarmCounter1 = 1 ' Create a timer. Set Timer1 = New App.Timer ' Create an event handler for the method that will process the timer event. Set Timer1EventHandler = New EventHandler(AddressOf Timer1EventProcessor) ' And connect it to the timer event. Timer1.AddOnTimer Timer1EventHandler ' Set the timer interval to 3 seconds. Timer1.Interval = 3000 ' Start the timer running. Timer1.Start ' And loop while the timer is running Do While exitFlag1 = False App.Application.DoEvents Loop End Sub
Timer Class | Timer Members | System.WinForms Namespace | RemoveOnTimer