What is the most timers you have used in an application? I think the most I have ever used is 2.
In another forum this question was asked. "I have a Timer ... I have a situation where I want to run the code in the Tick event ONLY ONCE not on the interval i have the timer set too."
I responded with
Timer1_Tick(New Object, New System.EventArgs)
Which led to this
"The 'sender' object is VERY important when you have more than one Timer being handled by the Tick event procedure. This is because in a multi-timer tick event procedure, the only way your program knows which timer just fired is by querying the 'sender' object.
When you use code like this you are putting nothing into the 'sender' object, so that if you were ever going to query it a 'kaboom' is going to happen."
"...this 9 timer Tick event handler job done. Remember, all that is required is the correct timer name gets put into the ListBox." "The need for multi-timers to be handled by a single Tick event handler happens all the time. How would a real life programmer handle this? "
Oh, to test the "kaboom" theory I wrote this and ran it overnight successfully:
Code:Public Class Form1 Dim run As Boolean = False Dim pickIT As Integer = 0 Dim lock As New Object Private Sub Form1_FormClosing(ByVal sender As Object, _ ByVal e As System.Windows.Forms.FormClosingEventArgs) _ Handles Me.FormClosing run = False Threading.Thread.Sleep(1000) End Sub Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load Timer1.Start() Timer2.Start() Timer3.Start() Timer4.Start() Timer5.Start() Timer6.Start() Timer7.Start() Timer8.Start() Timer9.Start() 'lets make it interesting by breaking all of ash2's rules 'shouldn't be hard to "Kaboom" according to him For x As Integer = 0 To 499 Dim t As New Threading.Thread(AddressOf PointlessForest) t.IsBackground = True t.Start() Next End Sub Private Sub PointlessForest() Do While Not run Threading.Thread.Sleep(100) Loop Do 'a little variety Threading.Monitor.Enter(lock) If pickIT > Integer.MaxValue \ 2 Then pickIT = 0 pickIT += 1 If (pickIT And 1) = 0 Then 'if pickit is even then Threading.Monitor.Exit(lock) _TickTock(New TextBox, New System.EventArgs) Else Dim foo As String = "For oblio " & pickIT.ToString Threading.Monitor.Exit(lock) _TickTock(foo, New System.EventArgs) End If Threading.Thread.Sleep(10) Loop While run End Sub Private Sub _TickTock(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles Timer1.Tick, Timer2.Tick, Timer3.Tick, _ Timer4.Tick, Timer5.Tick, Timer6.Tick, _ Timer7.Tick, Timer8.Tick, Timer9.Tick Dim t As System.Windows.Forms.Timer If TypeOf sender Is System.Windows.Forms.Timer Then t = DirectCast(sender, System.Windows.Forms.Timer) If ReferenceEquals(t, Timer1) Then Debug.WriteLine("T1") ElseIf ReferenceEquals(t, Timer2) Then Debug.WriteLine("T2") ElseIf ReferenceEquals(t, Timer3) Then Debug.WriteLine("T3") ElseIf ReferenceEquals(t, Timer4) Then Debug.WriteLine("T4") ElseIf ReferenceEquals(t, Timer5) Then Debug.WriteLine("T5") ElseIf ReferenceEquals(t, Timer6) Then Debug.WriteLine("T6") ElseIf ReferenceEquals(t, Timer7) Then Debug.WriteLine("T7") ElseIf ReferenceEquals(t, Timer8) Then Debug.WriteLine("T8") ElseIf ReferenceEquals(t, Timer9) Then Debug.WriteLine("T9") Else Stop End If Else Debug.WriteLine(sender.ToString) 'uncomment to test End If 'I am waiting on an answer? 'Debug.WriteLine(sender.name) 'this doesn't work 'Debug.WriteLine(t.name) 'name' is not a member of 'System.Windows.Forms.Timer'. End Sub Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click run = True End Sub End Class




Reply With Quote
