473,748 Members | 2,091 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 15064
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.Timer s (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(ByVa l sender As Object, _
ByVal e As System.EventArg s) 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.EventArg s)
TextBox1.Text = (CInt(TextBox1. Text) + 1).ToString
DirectCast(send er, System.Windows. Forms.Timer).En abled = True
End Sub
Public Sub mytimer2(ByVal sender As Object, _
ByVal e As System.EventArg s)
TextBox2.Text = (CInt(TextBox2. Text) + 1).ToString
DirectCast(send er, System.Windows. Forms.Timer).En abled = True
End Sub
Public Sub mytimer3(ByVal sender As Object, _
ByVal e As System.EventArg s)
TextBox3.Text = (CInt(TextBox3. Text) + 1).ToString
DirectCast(send er, System.Windows. Forms.Timer).En abled = True
End Sub
End Sub
///

"Gene Hubert" <gw******@hotma il.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******@hotma il.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******* *******@TK2MSFT NGP11.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.Timer s (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******* *******@TK2MSFT NGP11.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(ByV al message As String)
Private WithEvents frm2 As Form2
Private MyThread As System.Threadin g.Thread
Private Sub Form1_Load(ByVa l sender As Object, _
ByVal e As System.EventArg s) 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.EventArg s)
TextBox1.Text = (CInt(TextBox1. Text) + 1).ToString
DirectCast(send er, System.Windows. Forms.Timer).En abled = True
End Sub
Private Sub Button1_Click(B yVal sender As System.Object, _
ByVal e As System.EventArg s) Handles Button1.Click
frm1 = New Form2
frm1.itstop = Me.Top
frm1.itsleft = Me.Left + 200
frm1.Text = "Extra thread"
MyThread = New System.Threadin g.Thread(Addres sOf 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(Add ressOf 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(ByVa l message As String)
Friend itstop As Integer
Friend itsleft As Integer
Private Sub Form2_Activated (ByVal sender As Object, _
ByVal e As System.EventArg s) Handles MyBase.Activate d
Me.Left = itsleft
Me.Top = itstop
Me.BringToFront ()
Dim timenext As DateTime = Now.Add(TimeSpa n.FromSeconds(1 0))
Do While timenext > Now
TextBox1.Text = Now.TimeOfDay.T oString
Application.DoE vents() 'to show the time
Threading.Threa d.Sleep(50)
Me.Opacity -= 0.004
Loop
RaiseEvent form2ready(Now. TimeOfDay.ToStr ing)
End Sub
Private Sub Form2_Closing(B yVal sender As Object, ByVal _
e As System.Componen tModel.CancelEv entArgs) 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
2993
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 pretty simple, but I am missing it. NOTE: Server uses "CDO" ONLY. (Not CDONTS)
4
4042
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 to the next record. So, in the BeforeUpdate event of the form itself I have the following code:
3
1162
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 first form. How should this be handled? I know you can do it using the request object but I rather use the form elements if possible. I need to do this because my application is a multiple page form, where the next form is dynamic and depends on...
5
4694
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 change a value? As well, should I lock the array when I only want to read a value?
0
1097
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
3003
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 reference that record using VBA. However, I do not know how to create a recordset with that single record. I am using Access 2007. Thanks in advance.
10
9161
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 on the 'multiple items' form and therefore all fields change colour not just the current record. How do i refer to the current record with field named fieldname01? Thanks :)
0
1600
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 DataGridView control as well as some other items including a Button whose purpose is to start/stop a timer clock of sorts. The main problem here is that all of my controls are dynamically generated, so if there were 3 TabPages open, there would be 3 different...
4
1948
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 than one field or check muliple boxes to query the database. What I want to do is create a multiple search criteria data string. Below is the code that working with a the moment. Any input would be appreciate. Thanks Greg If...
0
9528
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9359
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8235
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6792
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4592
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4863
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3298
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2774
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.