0

I have a timer running in my web application. Each time the application starts up, the timer is created. The issue is that the app pool ends after an idle period which also ends the timer. The next request causes the app pool to start back up and a new timer is created.

Is there anyway to keep the timer from resetting?

user666423
  • 229
  • 3
  • 14
  • See this for better ways to achieve such a thing: http://stackoverflow.com/questions/542804/asp-netbest-way-to-run-scheduled-tasks – Shadow The GPT Wizard Mar 22 '11 at 18:07
  • What's wrong with the timer being initialised on startup? – Grant Thomas Mar 22 '11 at 18:08
  • The problem is the timer is set to run at 3am everyday. If the app pool recycles (due to idle) at 2:59:59am, there is no way the timer task would run. – user666423 Mar 22 '11 at 18:11
  • 1
    There is no way you can guarantee the application will be alive at that point, and although you can't with any type of system, this is what Windows Services are for! Have a web interface available on request, and a system service to carry out automated tasks. It just makes sense. – Grant Thomas Mar 22 '11 at 18:13
  • Thanks, I guess we'll move it to a scheduled task. – user666423 Mar 22 '11 at 18:19
  • We've solved this problem in a past with a tool that requests a specific url; we scheduled it to run daily, hourly, etc. with `at`. – configurator Mar 22 '11 at 19:37

1 Answers1

0

This is a question I see a lot. The short answer is no, the long answer is that even if you would periodically poll the web site it will eventually recycle the app pool anyway.

If need to do background work like this and embed that in ASP.NET you have to create a robust work queue that doesn't break if there are interruptions or crashes because it's going to happen. And that's just good design anyway for long running processes. This might seem like a lot of work but a simple design can take you very far.

The recommended approach is to pull that code into a separate Win32 service because the nature of such workloads don't sit well in REST based architectures.

If all you need is a periodic check, then it might be fine with just having an external script polling the web site but it's a crude way of handling timers.

John Leidegren
  • 59,920
  • 20
  • 131
  • 152
  • Another question (stackoverflow will only let me ask 1 question every 20 mins): I have health monitoring on my web application that monitors the application life cycle events. It logs an event each time the web app is compiled, started, and shutdown. The minInterval is set to 0. The issue is sometimes, there is duplicate events. For an example, just now, there is 3 compilation starting and compilation finished events that occurred at the same time. Shouldn't it log only one of each? – user666423 Mar 22 '11 at 18:18
  • 1
    Is twenty minutes really a dog's age to wait? ;) – Grant Thomas Mar 22 '11 at 18:23