472,365 Members | 1,746 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,365 software developers and data experts.

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
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.CenterParen t
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

Apr 11 '06 #1
4 10912
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
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?

Chris.

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?

Cor

"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?

Chris.

Apr 12 '06 #4
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.

Apr 12 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Peter Johnsson | last post by:
How come the eventhandler for the timer's elapsed time event is called over and over again, even though the AutoReset property is set to false, if you assign a new value to the timer objects...
10
by: WhiteSocksGuy | last post by:
Help! I am new to Visual Basic .Net (version 2002) and I am trying to get a System.Timers.Timer to work for me to display a splash screen for about two seconds and then load the main form. I have...
2
by: User | last post by:
Hi, What is the best way to release all resources holded by the Timer (myTimer from class System.Timers.Timer)? Is it: 1- myTimer.dispose 2- myTimer.enabled = false 3- myTimer.close
5
by: Michael C# | last post by:
Hi all, I set up a System.Timers.Time in my app. The code basically just updates the screen, but since the processing performed is so CPU-intensive, I wanted to make sure it gets updated...
0
by: brunft | last post by:
I was testing this with .NET Framework 2.0. Exceptions in the Elapsed event handler of a System.Timers.Timer are ignored, they are not handled by the runtime and not reported (when running...
0
by: archana | last post by:
Hi all, I am facing some wired problem while using system.timers.timer in windows service. I have one function which i want to execute once day. So i set interval of my timer to 60000 *1440. ...
7
by: RobKinney1 | last post by:
Hello, Wow...I have one for you all and hopefully I am not understanding this timer object correctly. I have a timer setup that pulses a connection through a socket every 60 seconds. But it...
5
by: Tony Gravagno | last post by:
I have a class that instantiates two Timer objects that fire at different intervals. My class can be instantiated within a Windows Form or from a Windows Service. Actions performed by one of the...
8
by: Ollie Riches | last post by:
I'm looking into a production issue related to a windows service and System.Timers.Timer. The background is the windows service uses a System.Timers.Timer to periodically poll a directory location...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.