How to prevent a System.Timers.Timer (Server timer) from getting killed or Prevent it from garbage collection
Can any one tell me how to prevent a server timer from getting killed on the server or from garbage collection.
ACTUAL PROBLEM:
I am using this server timer for a scheduling module which will run through my database tables and do some useful work. I have set "Autoreset=true" so that it repeats itself. Now the problem is from some days the timer is getting killed automatically on the server(Previously i used to kill it manually when the elapsed event occured). can any one tell me what the problem could be
Irfan AgariaPosted Sep 2, 2006, 6:47 AM
shailesh rajaPosted Sep 1, 2006, 12:03 PM
the following might help.. it's worked for me..
1. Ensure that the instance of the timer class in not declared locally .. say in onstart of a service or main().. You need to have the instance available at all times so that it is not garbage collected.
2. make sure that you catch exceptions in the timer callback function.. ontimer() and supress them.
3. make sure you have no long running operations in the timer callback function. instead you could spawn a new thread which would invoke the function that does the actual task. and that way you are giving back control ASAP to the .NET framework code that triggered the callback .
4. Avoid extremely small values for the time intreval while instantiating the timer class unless you have a good reason to do so.. a timer intreval of a couple of seconds or minute(s) is a safe bet..
Hope this helps...
Shailesh