473,404 Members | 2,195 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,404 software developers and data experts.

2 timers, doesnt work?

I am creating 2 timers inside a GUI, but it seems that only the one
declared last (the second timer), gets triggered, but the first one
doesnt.

Here is the code for the timers:
# main timer, which governs when the register watcher get updated
self.mainTimer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.UpdateAll, self.mainTimer)
self.mainTimer.Start(5, False)

# graph timer, which governs when the graph gets updated
self.grphTimer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.upGrphDisplay, self.grphTimer)
self.grphTimer.Start(101, False)

So in this case, only the upGrphDisplay method gets called, the
UpdateAll function doesnt get called.

Any clues as to why this is happening?

thanks a lot!
-- kiran

Jul 25 '06 #1
2 1402

Kiran wrote:
I am creating 2 timers inside a GUI, but it seems that only the one
declared last (the second timer), gets triggered, but the first one
doesnt.
You really should check the archives before posting. Exactly the same
question was asked less than a week ago.

The original question was answered by Nikie. I have quoted the reply
verbatim -

----------------------------------------------------------------
The problem is not that the first timer is stopped, the problem is
that both timers happen to call the same method in the end.

Think of the "Bind" method as an assignment: it assigns a handler
function to an event source. If you call it twice for the same event
source, the second call will overwrite the first event handler. That's
what happens in your code.
The easiest way to change this is by using different ids for the
timers:
def startTimer1(self):
self.t1 = wx.Timer(self, id=1)
self.t1.Start(2000)
self.Bind(wx.EVT_TIMER, self.OnUpTime, id=1)
def startTimer2(self):
self.t2 = wx.Timer(self, id=2)
self.t2.Start(1000)
self.Bind(wx.EVT_TIMER, self.OnTime, id=2)
This way, the timers launch two different events, which are bound to
two different methods.
----------------------------------------------------------------

I would suggest one minor change. Create a variable for the id using
wx.NewId(), and pass the variable as an argument in both places,
instead of hard-coding the id. That way you are guaranteed to get a
unique id. In a large project it can be difficult to keep track of
which id's have been used if they are all hard-coded.

Frank Millman

Jul 26 '06 #2
Makes sense Frank, and that seemed to work also, so thanks a lot!
Frank Millman wrote:
Kiran wrote:
I am creating 2 timers inside a GUI, but it seems that only the one
declared last (the second timer), gets triggered, but the first one
doesnt.

You really should check the archives before posting. Exactly the same
question was asked less than a week ago.

The original question was answered by Nikie. I have quoted the reply
verbatim -

----------------------------------------------------------------
The problem is not that the first timer is stopped, the problem is
that both timers happen to call the same method in the end.

Think of the "Bind" method as an assignment: it assigns a handler
function to an event source. If you call it twice for the same event
source, the second call will overwrite the first event handler. That's
what happens in your code.
The easiest way to change this is by using different ids for the
timers:
def startTimer1(self):
self.t1 = wx.Timer(self, id=1)
self.t1.Start(2000)
self.Bind(wx.EVT_TIMER, self.OnUpTime, id=1)
def startTimer2(self):
self.t2 = wx.Timer(self, id=2)
self.t2.Start(1000)
self.Bind(wx.EVT_TIMER, self.OnTime, id=2)
This way, the timers launch two different events, which are bound to
two different methods.
----------------------------------------------------------------

I would suggest one minor change. Create a variable for the id using
wx.NewId(), and pass the variable as an argument in both places,
instead of hard-coding the id. That way you are guaranteed to get a
unique id. In a large project it can be difficult to keep track of
which id's have been used if they are all hard-coded.

Frank Millman
Aug 1 '06 #3

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

Similar topics

0
by: Dmitry Demchuk | last post by:
Hi everybody. Recently I ran into situation with System.Threading.Timer in my ASP.NET application. I reinstalled Windows on one of my servers and got timers stop firing events after while, they...
3
by: Nathan Kovac | last post by:
I have a feeling I am missing something simple, but I just can't find it. Perhaps someone can give me a lead on where to look. I will describe the issue then post my code to the web service. My...
9
by: Mark Rae | last post by:
Hi, I've seen several articles about using System Timers in ASP.NET solutions, specifically setting them up in Global.asax' Application_OnStart event. I'm thinking about the scenario where I...
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...
6
by: Gene Hubert | last post by:
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. ...
1
by: Bamse | last post by:
Hi, can timers be used in webservices? as an example: to check at some time interval an object - Application and for each logged user, check its login period, and if it is greater than 30...
1
by: | last post by:
Frustrated.. (I have seen other posts regarding this problem with no resolution..) I am using dotnet 1.1 with latest SP on a Win2KP box (actually 2 boxes), have even run the service on WinXP SP2...
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...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.