Bug in timers.timer ?

G

Guest

Hi,

Maybe I’m doing something wrong but I can’t know what.

I’m using timers in my applications to do things after n seconds after do
any other thing.

Symptoms:
After a random time the Elapse event just stop to be raised. I tried to
debug and found something curious: Although I specified AutoReset property to
False, at some time in the Elapse event the Enabled property came at value of
True!!! After that happen, the Elapse event simply stop to rise. That means
that my host applications freeze will waiting some work to be done based on
that event.


Please analyze this problem. I created a simple example that demonstrates
that problem. The code is below this message. Just run it in debug mode,
click Start button and wait till form turn red. At that time the problem I
mentioned just happened. You can insert a breakpoint just after “If
T1.Enabled Then†line at “T1_Elapsed†sub and just after “If T2.Enabled Thenâ€
line at “T2_Elapsed†sub to analyze what is happening.
I couldn’t found an exact time that happened, so you can just start my
example application and do other things while wait form turn red, or
application stop at your break points.

In my example I had a button to restart timer after it stops raising events.
It seams that to restart raising Elapse events is just needed to set Enabled
property to false and call start again… But this behavior is not good!

If I start many instances of this example app, them the error appears more
offen.
Is that a bug?

Pedro Gonçalves

Example code:

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label4 As System.Windows.Forms.Label
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents lblCounter1 As System.Windows.Forms.Label
Friend WithEvents lblCounter2 As System.Windows.Forms.Label
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents Button3 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.Label1 = New System.Windows.Forms.Label()
Me.lblCounter1 = New System.Windows.Forms.Label()
Me.lblCounter2 = New System.Windows.Forms.Label()
Me.Label4 = New System.Windows.Forms.Label()
Me.Button1 = New System.Windows.Forms.Button()
Me.Button2 = New System.Windows.Forms.Button()
Me.Button3 = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(24, 32)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(112, 16)
Me.Label1.TabIndex = 0
Me.Label1.Text = "Elapse Counter"
Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'lblCounter1
'
Me.lblCounter1.BorderStyle =
System.Windows.Forms.BorderStyle.FixedSingle
Me.lblCounter1.FlatStyle = System.Windows.Forms.FlatStyle.Flat
Me.lblCounter1.Location = New System.Drawing.Point(24, 56)
Me.lblCounter1.Name = "lblCounter1"
Me.lblCounter1.Size = New System.Drawing.Size(112, 24)
Me.lblCounter1.TabIndex = 1
Me.lblCounter1.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter
'
'lblCounter2
'
Me.lblCounter2.BorderStyle =
System.Windows.Forms.BorderStyle.FixedSingle
Me.lblCounter2.FlatStyle = System.Windows.Forms.FlatStyle.Flat
Me.lblCounter2.Location = New System.Drawing.Point(160, 56)
Me.lblCounter2.Name = "lblCounter2"
Me.lblCounter2.Size = New System.Drawing.Size(112, 24)
Me.lblCounter2.TabIndex = 3
Me.lblCounter2.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter
'
'Label4
'
Me.Label4.Location = New System.Drawing.Point(160, 32)
Me.Label4.Name = "Label4"
Me.Label4.Size = New System.Drawing.Size(112, 16)
Me.Label4.TabIndex = 2
Me.Label4.Text = "Elapse Counter"
Me.Label4.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(24, 96)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(248, 32)
Me.Button1.TabIndex = 4
Me.Button1.Text = "Start"
'
'Button2
'
Me.Button2.Enabled = False
Me.Button2.Location = New System.Drawing.Point(28, 136)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(108, 32)
Me.Button2.TabIndex = 5
Me.Button2.Text = "Start Timer"
'
'Button3
'
Me.Button3.Enabled = False
Me.Button3.Location = New System.Drawing.Point(160, 136)
Me.Button3.Name = "Button3"
Me.Button3.Size = New System.Drawing.Size(108, 32)
Me.Button3.TabIndex = 6
Me.Button3.Text = "Start Timer"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(304, 175)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button3,
Me.Button2, Me.Button1, Me.lblCounter2, Me.Label4, Me.lblCounter1, Me.Label1})
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
Me.MaximizeBox = False
Me.Name = "Form1"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region

Private T1 As System.Timers.Timer
Private T2 As System.Timers.Timer

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
T1 = New System.Timers.Timer()
T1.AutoReset = False
T1.Interval = 10
T1.SynchronizingObject = Me
AddHandler T1.Elapsed, AddressOf T1_Elapsed

T2 = New System.Timers.Timer()
T2.AutoReset = False
T2.Interval = 500
T2.SynchronizingObject = Me
AddHandler T2.Elapsed, AddressOf T2_Elapsed

T1.Start()
T2.Start()

Button1.Enabled = False

End Sub

Private Sub T1_Elapsed(ByVal sender As Object, ByVal e As
System.Timers.ElapsedEventArgs) ' Handles T1.Elapsed
Static Counter As Integer

If T1.Enabled Then
' ????????????????
Me.BackColor = Color.Red
lblCounter1.BackColor = Color.LightYellow
Button2.Enabled = True
End If

Counter += 1
lblCounter1.Text = Counter
lblCounter1.Refresh()

T1.Start()

End Sub

Private Sub T2_Elapsed(ByVal sender As Object, ByVal e As
System.Timers.ElapsedEventArgs) ' Handles T2.Elapsed
Static Counter As Integer

If T2.Enabled Then
' ????????????????
Me.BackColor = Color.Red
lblCounter2.BackColor = Color.LightYellow
Button2.Enabled = True
End If

Counter += 1
lblCounter2.Text = Counter
lblCounter2.Refresh()

T2.Start()
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
T1.Enabled = False
T1.Start()
Button2.Enabled = False
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
T2.Enabled = False
T2.Start()
Button3.Enabled = False
End Sub

End Class
 
G

Guest

Hi Sijin,

I think you'll understand if you just run an application with the form class
below.
You'll see what's happening!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top