T
Tony Gravagno
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 event
handlers may take longer than the interval for either of the timers,
so it's possible for multiple events to fire "simultaneously" and for
events to queue up. I'm attempting to get the timers to sync on some
reference type object, or use System.Threading.Monitor to stop them
from re-entering code that's already being executed.
When a timer event fires I use Monitor.TryEnter(refobj) to attempt a
lock, and simply return on failure (in other words, don't bother
trying to do anything, we're already doing something). The refobj is
an instance of another one of my classes, but this doesn't appear to
be a suitable sync object.
I've tried making the object static/shared so the same object is
consistent across threads. Using this/me doesn't work. I can't use a
this/me reference to a Windows Form because the code could be
instantiated from a Service.
I am using similar code on methods invoked from the container - if a
timer event is doing some operation within the instance, the container
is informed that the object is busy, rather than making it wait for a
turn. If I can't lockdown the object then timers and user requests
are in contention, and the rest of the code outside of these entry
points is not thread-safe.
What type of object do I use to ensure Monitor.Enter(object) and
related methods will be respected by any thread? All of the classic
examples on the Net assume we can use 'this' or 'Me' and that we're
running in a Windows Form. This isn't valid for Windows Services,
especially when events like OnPause and OnContinue may get run from a
different thread than the one that started the process.
Thanks!
different intervals. My class can be instantiated within a Windows
Form or from a Windows Service. Actions performed by one of the event
handlers may take longer than the interval for either of the timers,
so it's possible for multiple events to fire "simultaneously" and for
events to queue up. I'm attempting to get the timers to sync on some
reference type object, or use System.Threading.Monitor to stop them
from re-entering code that's already being executed.
When a timer event fires I use Monitor.TryEnter(refobj) to attempt a
lock, and simply return on failure (in other words, don't bother
trying to do anything, we're already doing something). The refobj is
an instance of another one of my classes, but this doesn't appear to
be a suitable sync object.
I've tried making the object static/shared so the same object is
consistent across threads. Using this/me doesn't work. I can't use a
this/me reference to a Windows Form because the code could be
instantiated from a Service.
I am using similar code on methods invoked from the container - if a
timer event is doing some operation within the instance, the container
is informed that the object is busy, rather than making it wait for a
turn. If I can't lockdown the object then timers and user requests
are in contention, and the rest of the code outside of these entry
points is not thread-safe.
What type of object do I use to ensure Monitor.Enter(object) and
related methods will be respected by any thread? All of the classic
examples on the Net assume we can use 'this' or 'Me' and that we're
running in a Windows Form. This isn't valid for Windows Services,
especially when events like OnPause and OnContinue may get run from a
different thread than the one that started the process.
Thanks!