title: How to use System.Threading.Timer with asp.net web service
body: <p><strong>Background:</strong>
  I have a .NET 4.0 web service running on Windows Server 2008 written in C#.
  I would like to run a timer inside the asp.net web service that runs code once a day.  I have heard of some limitations with this (see below) but I am asking if what I plan is OK in order to get around some of the limitations and to keep the code inside the web service as it is not mission critical code.  However a crash is unacceptable.  Note: I am not and can not use WCF.</p>
  
  <p><strong>The plan:</strong></p>
  
  <ul>
  <li>Declare and instantiate <code>System.Threading.Timer</code> in the constructor of
  my web service (right next to the commented out line: 
  <code>//InitializeComponent();</code> ).</li>
  <li>Fire timer every 30 minutes.   </li>
  <li>Note: Because I am using <code>System.Threading.Timer</code> I should not have to run
  keep-alive code according to msdn Tip #6 below. </li>
  <li>IF the current time
  is within 30 minutes of my database value (desired time to run code)
  THEN run code.</li>
  </ul>
  
  <p><strong>Questions:</strong></p>
  
  <ul>
  <li>I would like to know how reliable it is to use <code>System.Threading.Timer</code> in asp.net c# code using the plan above and if it would run correctly let’s say more than 97% of the time?</li>
  <li>Will this plan work when the app pool is recycled and when IIS server is restarted?</li>
  </ul>
  
  <p>It seems there are issues using timers in web services with regards to:</p>
  
  <ul>
  <li>app pool recycling </li>
  <li>threads dying  </li>
  <li>no one hits web for a long period</li>
  <li>of time memory issues (better to use windows service)</li>
  </ul>
  
  <p><strong>References:</strong></p>
  
  <p><a href="https://stackoverflow.com/questions/8584909/asp-net-site-firing-some-code-at-a-specific-time">ASP.NET Site - Firing some code at a specific time</a>   </p>
  
  <p><a href="https://stackoverflow.com/questions/6008475/timer-on-website-to-activate-web-service-calls-every-hour">Timer on Website to activate Web Service calls every hour</a> </p>
  
  <p><a href="http://forums.asp.net/t/1079158.aspx/1" rel="nofollow noreferrer">http://forums.asp.net/t/1079158.aspx/1</a>  </p>
  
  <p><a href="http://msdn.microsoft.com/en-us/magazine/cc163854.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/magazine/cc163854.aspx</a>   (see Tip #6)</p>
  
  <p><strong>From Tip #6</strong>
  “The Timer class, found in the System.Threading namespace, is a wonderfully useful, but less well-known class in the .NET Framework, at least for Web developers. Once created, the Timer will invoke the specified callback on a thread from the ThreadPool at a configurable interval. This means you can set up code to execute without an incoming request to your ASP.NET application, an ideal situation for background processing. You can do work such as indexing or sending e-mail in this background process too.”</p>