title: Task Scheduler IIS side with C#
body: <p>I need to have tasks running in my web application. I have been reading and most of the answers are to use the task scheduler in Windows but what I need is to have it in the web application itself.</p>
  
  <p>I finally got something that almost works but I am still having some troubles:</p>
  
  <pre><code>using System;
  using System.Timers;
  using System.IO;
  
  namespace ServiceIIS
  {
      public static class Servicios
      {
          public static bool serviceStarted = false;
          static Timer serviceLoad = new Timer();
  
          public static void serviceLoader()
          {
              if (!serviceStarted)
              {
                  GC.KeepAlive(serviceStarted);
                  GC.KeepAlive(serviceLoad);
  
                  serviceStarted = true;
  
                  double Intervalo = loadData();
                  serviceLoad.Interval = Intervalo;
                  serviceLoad.Elapsed += new ElapsedEventHandler(serviceLoad_Elapsed);
                  serviceLoad.Enabled = true;
              }
          }
  
          static void serviceLoad_Elapsed(object sender, ElapsedEventArgs e)
          {
              serviceLoad.Stop();
              double Intervalo = loadData();
              serviceLoad.Interval = Intervalo;
              serviceLoad.Start();
          }
  
          static double loadData()
          {
              string dailyLoadTime = "00:00:00";
              string log = "";
              /*
                  loadData Code
                  log = resultMessage;
              */
  
              using (StreamWriter w = File.AppendText("c:\\log.txt"))
                  {
                      writeLog(log, w);
                      w.Close();
                  }
  
              return (Convert.ToDateTime(DateTime.Today.ToString("yyyy-MM-ddT") + dailyLoadTime).AddDays(1) - DateTime.Now).TotalMilliseconds;
          }
  
          public static void writeLog(string resultMessage, TextWriter w)
          {
              w.Write("\r\nLog : ");
              w.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(),
                  DateTime.Now.ToLongDateString());
              w.WriteLine("  :");
              w.WriteLine("  :{0}", resultMessage);
              w.WriteLine("-------------------------------");
              w.Flush();
          }
      }
  }
  </code></pre>
  
  <p>serviceLoader() will be called if serviceStarted is false, then I tell the garbage collector to ignore serviceStarted and serviceLoad to be cleaned.
  Calling loadData() for the first time to get the data and "generate" the interval for the timer to wait until loadData() is called again. Then I create the event handler and start the timer.</p>
  
  <p>Using the writeLog() procedure I know when loadData() is called. Waiting for the time it should be called (00:00:00 hours), there is no calling to loadData() but until a different time... that could be because the way I am computing the interval is not entirely correct.</p>
  
  <p>Then it is called again the same day and once the next day. Finally after the second day, there is no call at all for loadData() so I suppose it is because the garbage collector deleted it, calling serviceLoader() again, loadData() is called at the same time telling me that in effect serviceStarted has been cleaned and surely serveLoad has been cleaned too.</p>
  
  <p>Do you have an idea on what I am doing wrong and how could I get this working? I could not believe that there is no way to do this using IIS.</p>
  
  <p>Thank you in advance for your time reading this.</p>
  
  <p>Elder</p>