By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,699 Members | 1,402 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,699 IT Pros & Developers. It's quick & easy.

Multiple Timers in a Form

P: n/a
I seem to be getting crazy results when I have multiple
System.Windows.Forms.Timer objects in the same form running at the
same time. When only one timer is running I get the expected
behavior. When I have two timers running at once, one fires reliably
and the other fires almost never. Are there any known issues with
this?

Thanks,
Gene H.
Nov 21 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Forms timers work the same way in .NET as they did in VB6 (and earlier).
When a timer fires, ALL code in that timer must execute before any
additional timer events will be generated. If you have one timer that
executes code that takes as much time to execute (or more) than the timer
interval, then you will experience very erratic timing. If this is the
case, then you should consider use of either System.Timers, or
Threading.Timers (both with the attendant issues of threads and data
marshalling if they interact with the UI).

Dick

--
Richard Grier (Microsoft Visual Basic MVP)

See www.hardandsoftware.net for contact information.

Author of Visual Basic Programmer's Guide to Serial Communications, 4th
Edition ISBN 1-890422-28-2 (391 pages) published July 2004.
Nov 21 '05 #2

P: n/a
Gene,

I am made this little sample some days ago which I changed a little bit now
for you in this message. (Instead of the dynamicly build timers and the
addhandler you can as well use the normal dragged timer and the events using
the combobox, and than that directcast is not needed in the events but you
can take the timer itself)

I hope this helps?

Cor

\\\It needs on a form 3 buttons
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim timer1 As New System.Windows.Forms.Timer
TextBox1.Text = "0"
TextBox2.Text = "0"
TextBox3.Text = "0"
AddHandler timer1.Tick, AddressOf mytimer1
timer1.Enabled = True
timer1.Interval = 4000
Dim timer2 As New System.Windows.Forms.Timer
AddHandler timer2.Tick, AddressOf mytimer2
timer2.Enabled = True
timer2.Interval = 300
Dim timer3 As New System.Windows.Forms.Timer
AddHandler timer3.Tick, AddressOf mytimer3
timer3.Enabled = True
timer3.Interval = 20
End Sub
Public Sub mytimer1(ByVal sender As Object, _
ByVal e As System.EventArgs)
TextBox1.Text = (CInt(TextBox1.Text) + 1).ToString
DirectCast(sender, System.Windows.Forms.Timer).Enabled = True
End Sub
Public Sub mytimer2(ByVal sender As Object, _
ByVal e As System.EventArgs)
TextBox2.Text = (CInt(TextBox2.Text) + 1).ToString
DirectCast(sender, System.Windows.Forms.Timer).Enabled = True
End Sub
Public Sub mytimer3(ByVal sender As Object, _
ByVal e As System.EventArgs)
TextBox3.Text = (CInt(TextBox3.Text) + 1).ToString
DirectCast(sender, System.Windows.Forms.Timer).Enabled = True
End Sub
End Sub
///

"Gene Hubert" <gw******@hotmail.com>
I seem to be getting crazy results when I have multiple
System.Windows.Forms.Timer objects in the same form running at the
same time. When only one timer is running I get the expected
behavior. When I have two timers running at once, one fires reliably
and the other fires almost never. Are there any known issues with
this?

Thanks,
Gene H.

Nov 21 '05 #3

P: n/a

"Gene Hubert" <gw******@hotmail.com> wrote
I seem to be getting crazy results when I have multiple
System.Windows.Forms.Timer objects in the same form running at the
same time. When only one timer is running I get the expected
behavior. When I have two timers running at once, one fires reliably
and the other fires almost never. Are there any known issues with
this?

In addition to the other replies, you could try to use only one timer,
and watch the system clock to determine what should be done
at each Tick. If you need one second intervals, then you might
set the timer interval to 250. Watching for the change of the seconds
you would not (usually) be off more than 1/4 of a second.

But do note, as Dick indicated, it can happen that a Form timer
may not fire at all for some period of time, depending on how
busy the rest of the system is....

LFS
Nov 21 '05 #4

P: n/a
Thanks very much Dick.

It's the weirdest thing. The processing I'm doing in the timer tick
events is very minimal, e.g. setting a couple booleans or selecting
and then invoking edit mode for a record in a datagrid. From what you
say, it sounds like it should work fine.

If I disable either one of the two timers, the other one works
perfectly. But when they're both running at once, it's bizarre. I've
even had different results on different program runs without making
any code changes.

For now, I've combined the processing in the two timer tick events
into a single timer tick handler sub with conditional processing based
on booleans and counters. This lets me run with just one timer.
After initial testing it looks like it's doing the job.

I was doing a lot of starting and stopping of the two timers. I
wonder if that could cause problems.

Gene H.

"Dick Grier" <di**************@msn.com> wrote in message news:<uV**************@TK2MSFTNGP11.phx.gbl>...
Forms timers work the same way in .NET as they did in VB6 (and earlier).
When a timer fires, ALL code in that timer must execute before any
additional timer events will be generated. If you have one timer that
executes code that takes as much time to execute (or more) than the timer
interval, then you will experience very erratic timing. If this is the
case, then you should consider use of either System.Timers, or
Threading.Timers (both with the attendant issues of threads and data
marshalling if they interact with the UI).

Dick

Nov 21 '05 #5

P: n/a
Thanks Cor.

I tried something similar with a very simple form with a couple timers
runing at the same time and not see any problems with it.

For some reason I get very different results in my app with two timers
running at the same time where the context is more complex.

Gene H.

"Cor Ligthert" <no************@planet.nl> wrote in message news:<e8**************@TK2MSFTNGP11.phx.gbl>...
Gene,

I am made this little sample some days ago which I changed a little bit now
for you in this message. (Instead of the dynamicly build timers and the
addhandler you can as well use the normal dragged timer and the events using
the combobox, and than that directcast is not needed in the events but you
can take the timer itself)

I hope this helps?

Cor

\\\It needs on a form 3 buttons


....
Nov 21 '05 #6

P: n/a
Gene,

When there is a proces in your program running that takes some time and
there are no events in it, let say a process that reads a very big dataset
from a database, than the timerevents are passed.

The only posibility you have than in my opinion is to use multithreading.

I made this sample yesterday. In this case multithreading is not needed
because there are enough events fired in the process. However when that is
not, you should use that multithreading.

With some exceptions, by instance for more threads at one time, is as I
thougth everything in it for an extra single thread. (When you are
processing the same datafields in the mainthread and the seperated thread
you have to look to synclock, however when you can avoid that you should do
that).

I hope this helps?

Cor

\\\needs on form 1 one button and three textboxes
Private WithEvents frm1 As Form2
Private Delegate Sub Frm1Handler(ByVal message As String)
Private WithEvents frm2 As Form2
Private MyThread As System.Threading.Thread
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim timer1 As New System.Windows.Forms.Timer
AddHandler timer1.Tick, AddressOf mytimer1
TextBox1.Text = "0"
timer1.Enabled = True
timer1.Interval = 400
Dim timer2 As New System.Windows.Forms.Timer
End Sub
Private Sub mytimer1(ByVal sender As Object, _
ByVal e As System.EventArgs)
TextBox1.Text = (CInt(TextBox1.Text) + 1).ToString
DirectCast(sender, System.Windows.Forms.Timer).Enabled = True
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
frm1 = New Form2
frm1.itstop = Me.Top
frm1.itsleft = Me.Left + 200
frm1.Text = "Extra thread"
MyThread = New System.Threading.Thread(AddressOf frm1.Show)
MyThread.Start()
frm2 = New Form2
frm2.itstop = Me.Top
frm2.itsleft = Me.Left + 400
frm2.Text = "In own thread"
frm2.Show()
End Sub
Private Sub Frm1Ready(ByVal message As String)
Me.BeginInvoke(New Frm1Handler(AddressOf Frm1HandlerSub), New
Object() {message})
End Sub
Private Sub Frm1HandlerSub(ByVal message As String)
TextBox2.Text = message
frm1.Close()
MyThread.Abort()
End Sub
Private Sub frm2ready(ByVal message As String) _
Handles frm2.form2ready
TextBox3.Text = message
frm2.Dispose()
End Sub
///
\\\Needs a form2 with one textbox
Friend Event form2ready(ByVal message As String)
Friend itstop As Integer
Friend itsleft As Integer
Private Sub Form2_Activated(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Activated
Me.Left = itsleft
Me.Top = itstop
Me.BringToFront()
Dim timenext As DateTime = Now.Add(TimeSpan.FromSeconds(10))
Do While timenext > Now
TextBox1.Text = Now.TimeOfDay.ToString
Application.DoEvents() 'to show the time
Threading.Thread.Sleep(50)
Me.Opacity -= 0.004
Loop
RaiseEvent form2ready(Now.TimeOfDay.ToString)
End Sub
Private Sub Form2_Closing(ByVal sender As Object, ByVal _
e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
e.Cancel = True
End Sub
///
Nov 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.