I have a Timer which kicks off and does it’s job indefinitely in it’s own thread until something happens in the main thread to disable it, in which case a different one will be enabled. This works perfectly. The only problem is that I want to make COMPLETELY sure the second timer doesn’t run for too long.
Is there a way to make a timer automatically disable after, say, 10 minutes if it doesn’t receive a specific shutdown command due to some malfunction?
I see that the class has a InitializeLifetimeService method. It sounds like it could help but I have no idea how to work it.
Thanks guys and gals 🙂
You might consider starting a third Timer when you start the second one to disable it after a given time.
The example above makes use of System.Timers.Timer by the way. If you are using System.Threading.Timer you might consider a switch there as well for surprisingly enough System.Timers.Timer is the one who is threadsafe by nature, where System.Threading.Timer is not.
Quite good overview can be found here:
http://msdn.microsoft.com/en-us/magazine/cc164015.aspx
EDIT for clarity:
the line
could also be written as
followed by
which is what you actually get when hitting Ctrl+Space in Visual Studio, given hat you have access to t1 in the lower method.