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.