Home All Groups Group Topic Archive Search About

System.Timers.Timer.Elapsed event not firing

Author
11 Apr 2006 4:12 AM
Liverpool fan
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
system.timers.timer
        '                   forms timer has a bug which causes high cpu
usage
        '

'******************************************************************************
        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()
            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 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
InitializeComponent()
            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
System.Windows.Forms.ToolTip(Me.components)
            Me.SuspendLayout()
            '
            'butRunNow
            '
            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")
            '
            'butCancel
            '
            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")
            '
            'txtRemaining
            '
            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"
            '
            'ZShadowTitle1
            '
            Me.ZShadowTitle1.BackColor =
System.Drawing.SystemColors.Control
            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
            '
            'dlgZTimer
            '
            Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
            Me.ClientSize = New System.Drawing.Size(298, 151)
            Me.Controls.Add(Me.ZShadowTitle1)
            Me.Controls.Add(Me.txtRemaining)
            Me.Controls.Add(Me.butCancel)
            Me.Controls.Add(Me.butRunNow)
            Me.FormBorderStyle =
System.Windows.Forms.FormBorderStyle.FixedDialog
            Me.MaximizeBox = False
            Me.MinimizeBox = False
            Me.Name = "dlgZTimer"
            Me.ShowInTaskbar = False
            Me.StartPosition =
System.Windows.Forms.FormStartPosition.CenterParent
            Me.Text = "Timer"
            Me.ResumeLayout(False)

        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
            Me.Close()
        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
            Me.Close()
        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

            UpdateDisplay()
            Me.Cursor = Cursors.WaitCursor
            Timer1.Start()

        End Sub

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

            UpdateDisplay()
            If Date.Now > TargetTime Then
                Me.DialogResult = DialogResult.OK
                Me.Close()
            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

Author
11 Apr 2006 6:02 AM
Cor Ligthert [MVP]
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).

Cor
Author
11 Apr 2006 10:41 PM
Liverpool fan
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?

Chris.
Author
12 Apr 2006 5:42 AM
Cor Ligthert [MVP]
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?

Cor

Show quoteHide quote
"Liverpool fan" <chris.no***@feltex.com> schreef in bericht
news:1144795287.917386.312810@i40g2000cwc.googlegroups.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?
>
> Chris.
>
Author
12 Apr 2006 6:00 AM
Liverpool fan
Cor,

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.

Chris.