473,378 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Multiple Timers in a Form

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
6 14965
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
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

"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mark Creelman | last post by:
Hello all: How do I convert this ASP mailer script to accept and mail multiple fields of a form. This will do one field only )iMesg.TextBody = Request.Form("body") and it works great. Seems...
4
by: William Wisnieski | last post by:
Hello Everyone, Access 2000 I have a form with multiple pages on it. There is one text field on the third page of the form that I need the user to complete before leaving the form or moving...
3
by: Bill | last post by:
Is there a way for a web form to submit to another web form and still use the form elements like text box's etc..? It appears that the second page would not know what form elements were on the...
5
by: FredC | last post by:
I have an application that uses mulitple timers. Each of the timer event handlers manipulate a common array of data. I'm getting Null refererance errors - should I put a lock on the array when I...
0
by: Stanley | last post by:
For a multiple independent form application of VC.net 2003, how to program a button in Form 2 to make it have the ability to jump back to the starting form ?
2
by: jamieda | last post by:
I have a multiple items form displaying the contents of a table. It has a primary key and the records are ordered by this. I want to be able to manually select a record in the form and then...
10
reginaldmerritt
by: reginaldmerritt | last post by:
i wish to change the backcolor of a field on a current record being dispalyed in a 'multiple items' form. However when using me.fieldname01.backcolor = makes refrence to all fields called fieldname01...
0
by: Kabo | last post by:
Hi there, I'm currently working on a C# application that deals with dynamically created TabPages based (each TabPage represents a file that has been opened). On each TabPage there is a...
4
by: gblack301 | last post by:
Hi, I have a search form where the user can check a box or enter some data such as a name to quey the database. I was wondering what is the best way to enable the ability for a user data in more...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.