473,396 Members | 1,891 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,396 software developers and data experts.

static timers

I need some insight into timers, the static modifier and instance memory
safety.
public class myClass
{
protected static int i = 0;
portected float myfloat;
protected static Timer staticTimer = new Timer();
}

public class myObject: myClass
{
public myClass()
{
staticTimer.Elapsed+=ElapsedEventHandler(this.onSt aticTimerEvent);
staticTimer.Interval = 999;
staticTimer.enabled = true;
}
private void onStaticTimerEvent( ...)
{
lock(i)
{
i++;
myFLoat +=i;
}
}
}

Assume I create 100 instances of myObject. I assume there is only one
staticTimer. So how do the 100 instances interact with only one timer
instance? And do I need the lock on (i) - I would assume that since there is
only one timer, onStaticTimerEvent (specificlly i) would be thread safe from
other instances.

Is it practice to have the timer declared and instantiated within the
instance? In this case, one hundred staticTimers would exist and the lock(i)
would be needed.
May 23 '06 #1
7 5632
Hi,

"FredC" <Fr***@discussions.microsoft.com> wrote in message
news:B5**********************************@microsof t.com...
I need some insight into timers, the static modifier and instance memory
safety.
Why are they static in the first place?
Assume I create 100 instances of myObject. I assume there is only one
staticTimer.
YES, and you will reset it each time you create a new instance
So how do the 100 instances interact with only one timer
instance? And do I need the lock on (i) - I would assume that since there
is
only one timer, onStaticTimerEvent (specificlly i) would be thread safe
from
other instances.
yes, you will eventually have a lot of waiting, everybody will be waiting
for i to unlock

Is it practice to have the timer declared and instantiated within the
instance? In this case, one hundred staticTimers would exist and the
lock(i)
would be needed.

What is what you want to do? The current solution is FAR from perfect.

--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
May 23 '06 #2
Hello FredC,

Firstly, take into account register/unregister events are thread-safe if
its called from outside the class with the event.
If u call it within the class you must provide thread safety yourself by
calling += or -= within a lock statement.

Secondly, int is an atom statement, for most cases you shouldn't syncronize
it

F> I need some insight into timers, the static modifier and instance
F> memory
F> safety.
F> public class myClass
F> {
F> protected static int i = 0;
F> portected float myfloat;
F> protected static Timer staticTimer = new Timer();
F> }
F> public class myObject: myClass
F> {
F> public myClass()
F> {
F>
F> staticTimer.Elapsed+=ElapsedEventHandler(this.onSt aticTimerEvent);
F> staticTimer.Interval = 999;
F> staticTimer.enabled = true;
F> }
F> private void onStaticTimerEvent( ...)
F> {
F> lock(i)
F> {
F> i++;
F> myFLoat +=i;
F> }
F> }
F> }
F> Assume I create 100 instances of myObject. I assume there is only one
F> staticTimer. So how do the 100 instances interact with only one timer
F> instance? And do I need the lock on (i) - I would assume that since
F> there is only one timer, onStaticTimerEvent (specificlly i) would be
F> thread safe from other instances.
F>
F> Is it practice to have the timer declared and instantiated within the
F> instance? In this case, one hundred staticTimers would exist and the
F> lock(i) would be needed.
F>
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
May 23 '06 #3
FredC,

See inline:
Assume I create 100 instances of myObject. I assume there is only one
staticTimer. So how do the 100 instances interact with only one timer
instance? And do I need the lock on (i) - I would assume that since there
is
only one timer, onStaticTimerEvent (specificlly i) would be thread safe
from
other instances.
The first thing that strikes me as odd is that you have a method named
myClass in your class. I think you meant myObject, and you meant to have it
be the constructor.

You would also be setting the interval for the timer every time you
create a class. Generally speaking, you should do this once, maybe in the
static constructor of the type that has a reference to the timer.

On top of that, you will probably get an exception somewhere along the
line. The reason for this is that you are boxing the int i before you pass
it to the lock. When you exit the lock, the boxed instance is different,
and you are trying to exit a block with a different object reference (one
that hasn't been locked on).

Finally, every instance of this class that you create is never going to
be garbage collected. The reason for this is that the class registers the
event handler with the static timer, which keeps the reference alive as long
as the timer is alive. Because it is static, it will shut down when the app
shuts down.

Needless to say, this is not optimal.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

Is it practice to have the timer declared and instantiated within the
instance? In this case, one hundred staticTimers would exist and the
lock(i)
would be needed.

May 23 '06 #4
FredC,

This code won't produce the result you probably expect.

Your timer variable is static, but you create the timer and assigned the
variable in the instance constructor. That means every new instance of my
class will create new timer and will assigned to the static variable thus
overriding the old reference. As long as there is no reference to the old
timer anymore it will eventually finalized and garbage collected. Upon
finalization the timer will be disabled, but until that you are going to
receive ticks from it.

If you fix the problem I just explained to you and since the event handler
is instance method each MyClass object will receive notification from the
timer. The event handler will be executed one by one from the same thread
pool thread. That means if the timer event is the only one that is modifying
this variable you are safe. Depending on the time needed to process the
event and the number of event handlers the timer might appear to be not
accurate enough.

--
HTH
Stoitcho Goutsev (100)

"FredC" <Fr***@discussions.microsoft.com> wrote in message
news:B5**********************************@microsof t.com...
I need some insight into timers, the static modifier and instance memory
safety.
public class myClass
{
protected static int i = 0;
portected float myfloat;
protected static Timer staticTimer = new Timer();
}

public class myObject: myClass
{
public myClass()
{
staticTimer.Elapsed+=ElapsedEventHandler(this.onSt aticTimerEvent);
staticTimer.Interval = 999;
staticTimer.enabled = true;
}
private void onStaticTimerEvent( ...)
{
lock(i)
{
i++;
myFLoat +=i;
}
}
}

Assume I create 100 instances of myObject. I assume there is only one
staticTimer. So how do the 100 instances interact with only one timer
instance? And do I need the lock on (i) - I would assume that since there
is
only one timer, onStaticTimerEvent (specificlly i) would be thread safe
from
other instances.

Is it practice to have the timer declared and instantiated within the
instance? In this case, one hundred staticTimers would exist and the
lock(i)
would be needed.

May 23 '06 #5
Where do you see that it will create a new instance of the timer every
time myObject is created? It will be created once, the first time the type
is accessed.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Stoitcho Goutsev (100)" <10*@100.com> wrote in message
news:e2**************@TK2MSFTNGP03.phx.gbl...
FredC,

This code won't produce the result you probably expect.

Your timer variable is static, but you create the timer and assigned the
variable in the instance constructor. That means every new instance of my
class will create new timer and will assigned to the static variable thus
overriding the old reference. As long as there is no reference to the old
timer anymore it will eventually finalized and garbage collected. Upon
finalization the timer will be disabled, but until that you are going to
receive ticks from it.

If you fix the problem I just explained to you and since the event handler
is instance method each MyClass object will receive notification from the
timer. The event handler will be executed one by one from the same thread
pool thread. That means if the timer event is the only one that is
modifying this variable you are safe. Depending on the time needed to
process the event and the number of event handlers the timer might appear
to be not accurate enough.

--
HTH
Stoitcho Goutsev (100)

"FredC" <Fr***@discussions.microsoft.com> wrote in message
news:B5**********************************@microsof t.com...
I need some insight into timers, the static modifier and instance memory
safety.
public class myClass
{
protected static int i = 0;
portected float myfloat;
protected static Timer staticTimer = new Timer();
}

public class myObject: myClass
{
public myClass()
{
staticTimer.Elapsed+=ElapsedEventHandler(this.onSt aticTimerEvent);
staticTimer.Interval = 999;
staticTimer.enabled = true;
}
private void onStaticTimerEvent( ...)
{
lock(i)
{
i++;
myFLoat +=i;
}
}
}

Assume I create 100 instances of myObject. I assume there is only one
staticTimer. So how do the 100 instances interact with only one timer
instance? And do I need the lock on (i) - I would assume that since
there is
only one timer, onStaticTimerEvent (specificlly i) would be thread safe
from
other instances.

Is it practice to have the timer declared and instantiated within the
instance? In this case, one hundred staticTimers would exist and the
lock(i)
would be needed.


May 23 '06 #6
FredC wrote:
I need some insight into timers, the static modifier and instance memory
safety.
public class myClass
{
protected static int i = 0;
portected float myfloat;
protected static Timer staticTimer = new Timer();
}

public class myObject: myClass
{
public myClass()
{
staticTimer.Elapsed+=ElapsedEventHandler(this.onSt aticTimerEvent);
staticTimer.Interval = 999;
staticTimer.enabled = true;
}
private void onStaticTimerEvent( ...)
{
lock(i)
{
i++;
myFLoat +=i;
}
}
}

Assume I create 100 instances of myObject. I assume there is only one
staticTimer. So how do the 100 instances interact with only one timer
instance?
Yes, there is only one Timer object. The event handlers will each be
started using a separate thread. As they all start at once, the system
might get quite sluggish when they do.

You might want to limit the number of handlers per timer, or fire the
timer more often and use a dispatcher to call the handlers on a rotating
scheme.
And do I need the lock on (i) - I would assume that since there is
only one timer, onStaticTimerEvent (specificlly i) would be thread safe from
other instances.
No, the timer starts the delegate in a new thread.

A lock on the variable i is totally worthless, though. What you actually
will be doing is copying the value of the variable and box it inside a
new object on the heap, and perform the lock on that object. As each
thread would create a new object to do the lock on, the locking has no
effect what so ever. You have to use a reference type variable for the
locking.
Is it practice to have the timer declared and instantiated within the
instance? In this case, one hundred staticTimers would exist and the lock(i)
would be needed.


If you want to use a static timer, add a proper static constructor to
the class where you initialize the timer.

A lock will be needed regardless if you have one or a hundred timers.
May 23 '06 #7
Oops, I'm sorry.
Just ignore my fitst comment. I thought I saw 'new Timer()' in the instnace
constructor. I was wrong.
--

Stoitcho Goutsev (100)

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:ek**************@TK2MSFTNGP05.phx.gbl...
Where do you see that it will create a new instance of the timer every
time myObject is created? It will be created once, the first time the
type is accessed.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Stoitcho Goutsev (100)" <10*@100.com> wrote in message
news:e2**************@TK2MSFTNGP03.phx.gbl...
FredC,

This code won't produce the result you probably expect.

Your timer variable is static, but you create the timer and assigned the
variable in the instance constructor. That means every new instance of my
class will create new timer and will assigned to the static variable thus
overriding the old reference. As long as there is no reference to the old
timer anymore it will eventually finalized and garbage collected. Upon
finalization the timer will be disabled, but until that you are going to
receive ticks from it.

If you fix the problem I just explained to you and since the event
handler is instance method each MyClass object will receive notification
from the timer. The event handler will be executed one by one from the
same thread pool thread. That means if the timer event is the only one
that is modifying this variable you are safe. Depending on the time
needed to process the event and the number of event handlers the timer
might appear to be not accurate enough.

--
HTH
Stoitcho Goutsev (100)

"FredC" <Fr***@discussions.microsoft.com> wrote in message
news:B5**********************************@microsof t.com...
I need some insight into timers, the static modifier and instance memory
safety.
public class myClass
{
protected static int i = 0;
portected float myfloat;
protected static Timer staticTimer = new Timer();
}

public class myObject: myClass
{
public myClass()
{

staticTimer.Elapsed+=ElapsedEventHandler(this.onSt aticTimerEvent);
staticTimer.Interval = 999;
staticTimer.enabled = true;
}
private void onStaticTimerEvent( ...)
{
lock(i)
{
i++;
myFLoat +=i;
}
}
}

Assume I create 100 instances of myObject. I assume there is only one
staticTimer. So how do the 100 instances interact with only one timer
instance? And do I need the lock on (i) - I would assume that since
there is
only one timer, onStaticTimerEvent (specificlly i) would be thread safe
from
other instances.

Is it practice to have the timer declared and instantiated within the
instance? In this case, one hundred staticTimers would exist and the
lock(i)
would be needed.



May 23 '06 #8

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

Similar topics

3
by: Jeff Greenland | last post by:
Hello everyone, I am having problems with Timers in a web application. They just seem to stop running after 15 minutes or so. My web application is set up like this: When a user hits a...
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...
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...
9
by: N! Xau | last post by:
hi I need a way to threat a certain number of timers in a homogeneous way. Let's say I have this code: select X case 1 timer1.enabled = true case 2
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...
1
by: Jonathan Woods | last post by:
Hi there, I have three methods these need to execute at every interval time. I would like to know which option is better? Option A) Three System.Timers.Timer objects these execute each...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
0
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,...

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.