Consider this code…
using System.Threading; //... Timer someWork = new Timer( delegate(object state) { //Do some work here... }, null, 0, 60000); HttpContext.Current.Application['SomeWorkItem'] = someWork;
Could this be dangerous? Caching a timer in the Application to perform some work in the background while your site runs seems safe, but I wondered if anyone has some experience with this.
I’m sure that writing a Service to run in the background would certainly be much better, but sometimes that isn’t always an option. Is this an alternative?
This would generally be a bad idea, as System.Threading.Timer uses threads from the ThreadPool, the same as ASP.Net.
If for what ever reason your timer delegate blocks or stops, the timer will simply begin a new Thread after the timeout period, which eats in to the Threads available for ASP.net.
If they all begin blocking, effectively you will not be able to serve any more web requests (probably a bad thing)