469,934 Members | 1,884 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,934 developers. It's quick & easy.

System.Timers.Timer.Elapsed event not firing

I have a windows application written using VB .NET that encompasses a
countdown timer modal dialog. The timer is a System.Timers.Timer with
an interval of 1 second. AutoReset is not set so accepts the default of
True. The Elapsed event handler updates the dialog box with how long
before it will close, acting as a timer itself. The dialog has a time
to close property which is checked every time the Elapsed event fires.

The problem I have is that this application has been working
successfully for well over a year. I am now in the process of moving it
to a new faster server and I am now getting an intermittent error. The
dialog timer is set for 1 minute intervals and therefore the Elapsed
event should fire 60 times, however after the application has been
running for an indeterminate time, the timer will only fire once.
Nowhere am I setting the AutoReset property to False.

I guess that the problem is with the new machine but I don't know where
to start and can't find anybody else who has had a similar problem.

Here is the dialog class.

Imports System.windows.forms

Namespace WinForms
Public Class dlgZTimer

'************************************************* *****************************
' Displays a timer dialog
' Modification Log
' ================
' 16.12.2003 mike Replace system.windows.forms.timer with
' forms timer has a bug which causes high cpu

'************************************************* *****************************
Inherits System.Windows.Forms.Form

Public TimerTitle As String
Public TimerIntervalSecs As Long
Public TargetTimeIntervalMins As Double
Public TargetTime As Date

Private nSecs As Long
Private nMins As Long
Private nHours As Long

Dim WithEvents Timer1 As System.Timers.Timer
#Region " Windows Form Designer generated code "

Public Sub New()

'This call is required by the Windows Form Designer.

'Add any initialization after the InitializeComponent()

End Sub

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

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

'NOTE: The following procedure is required by the Windows Form
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents butRunNow As System.Windows.Forms.Button
Friend WithEvents butCancel As System.Windows.Forms.Button
Friend WithEvents txtRemaining As System.Windows.Forms.TextBox
Friend WithEvents ZShadowTitle1 As Fehon.WinForms.ZShadowTitle
Friend WithEvents ToolTip1 As System.Windows.Forms.ToolTip

<System.Diagnostics.DebuggerStepThrough()> Private Sub
Me.components = New System.ComponentModel.Container
Me.butRunNow = New System.Windows.Forms.Button
Me.butCancel = New System.Windows.Forms.Button
Me.txtRemaining = New System.Windows.Forms.TextBox
Me.ZShadowTitle1 = New Fehon.WinForms.ZShadowTitle
Me.ToolTip1 = New
Me.butRunNow.Location = New System.Drawing.Point(217, 76)
Me.butRunNow.Name = "butRunNow"
Me.butRunNow.TabIndex = 0
Me.butRunNow.Text = "Run Now"
Me.ToolTip1.SetToolTip(Me.butRunNow, "Stop the timer and
run the process now")
Me.butCancel.Location = New System.Drawing.Point(217, 110)
Me.butCancel.Name = "butCancel"
Me.butCancel.TabIndex = 1
Me.butCancel.Text = "Cancel"
Me.ToolTip1.SetToolTip(Me.butCancel, "Cancel the Timer")
Me.txtRemaining.Enabled = False
Me.txtRemaining.Font = New System.Drawing.Font("Microsoft
Sans Serif", 15.75!, System.Drawing.FontStyle.Regular,
System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.txtRemaining.Location = New System.Drawing.Point(179, 8)
Me.txtRemaining.Name = "txtRemaining"
Me.txtRemaining.Size = New System.Drawing.Size(113, 35)
Me.txtRemaining.TabIndex = 2
Me.txtRemaining.Text = "hh:mm:ss"
Me.ZShadowTitle1.BackColor =
Me.ZShadowTitle1.CausesValidation = False
Me.ZShadowTitle1.Location = New System.Drawing.Point(4, 8)
Me.ZShadowTitle1.Name = "ZShadowTitle1"
Me.ZShadowTitle1.ShadowTitle = "Waiting until Sunday
23-Mar-2002 10:00:00 AM"
Me.ZShadowTitle1.Size = New System.Drawing.Size(172, 136)
Me.ZShadowTitle1.TabIndex = 3
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(298, 151)
Me.FormBorderStyle =
Me.MaximizeBox = False
Me.MinimizeBox = False
Me.Name = "dlgZTimer"
Me.ShowInTaskbar = False
Me.StartPosition =
System.Windows.Forms.FormStartPosition.CenterParen t
Me.Text = "Timer"

End Sub

#End Region

Private Sub butCancel_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles butCancel.Click
Me.Cursor = Cursors.Default
Me.DialogResult = DialogResult.Cancel
End Sub

Private Sub butRunNow_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles butRunNow.Click
Me.Cursor = Cursors.Default
Me.DialogResult = DialogResult.OK
End Sub

Private Sub dlgZTimer_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load

If TimerTitle <> "" Then
Me.Text = TimerTitle
End If

' Default Timer Interval is 1 second
Timer1 = New System.Timers.Timer
Timer1.Interval = 1 * 1000
If TimerIntervalSecs <> 0 Then
Timer1.Interval = CInt(TimerIntervalSecs * 1000)
End If

' Add minutes to get target time
If TargetTimeIntervalMins <> 0 Then
nSecs = CInt(TargetTimeIntervalMins * 60)
TargetTime = DateAdd("s", nSecs, Date.Now)
End If

Me.Cursor = Cursors.WaitCursor

End Sub

Private Sub Timer1_Elapsed(ByVal sender As Object, ByVal e As
System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed

If Date.Now > TargetTime Then
Me.DialogResult = DialogResult.OK
End If

End Sub

Private Sub UpdateDisplay()
ZShadowTitle1.ShadowTitle = "Waiting until " &
Format(TargetTime, "dddd dd-MMM-yyyy hh:mm:ss tt")

nSecs = DateDiff("s", Date.Now, TargetTime)
nMins = CLng(Int(CSng(nSecs / 60)))
nHours = CLng(Int(CSng(nMins / 60)))
nSecs = nSecs Mod 60
nMins = nMins Mod 60
txtRemaining.Text = Format(nHours, "00") & ":" &
Format(nMins, "00") & ":" & Format(nSecs, "00")
End Sub

End Class
End Namespace

Apr 11 '06 #1
4 10679
Hi Reds fan,

What bug of the Forms Timer are you talking about in your code. In my idea
is that the smoothest Timer, however only if you use it in a windows form.

(And don't forget to disable it in the begin of its event and to start it
again in the end of that).

Apr 11 '06 #2
Hi Cor,

I am not using the forms timer. I am using System.Timers.Timer.
The problem I have, is that intermittently it will only fire once. I've
read somewhere that you can't use it in a windows service, but this a
windows application. Is this a bug?


Apr 11 '06 #3
Liverpool fan,

I don't know, however despite what I have read as probably you did as well,
do I use the form timer if possible, it is much easier and I did not see
problems with that yet. Therefore was my question meant as. Why don't you
try that one?


"Liverpool fan" <ch*********@feltex.com> schreef in bericht
news:11**********************@i40g2000cwc.googlegr oups.com...
Hi Cor,

I am not using the forms timer. I am using System.Timers.Timer.
The problem I have, is that intermittently it will only fire once. I've
read somewhere that you can't use it in a windows service, but this a
windows application. Is this a bug?


Apr 12 '06 #4

As you will see from the change history on the code I've posted, we've
had problems with the Forms timer in the past. I've resorted to using
the Thread Timer instead. 3rd time lucky I hope. It's less intuitive
than the other 2 but seems to work okay, so far.


Apr 12 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Peter Johnsson | last post: by
5 posts views Thread by Michael C# | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.