title: Repeated Tasks using Timer Interval vs Task Delay
body: <p>I'm implementing a scheduled job like methods and have narrowed down to approaches. One that implements a Timer Interval and the other based on Task Delay. </p>
  
  <p>I've also considered using Azure Timer triggered webjobs but they do not work in a multi-instance mode. Effectively in a multi-instance application only one trigger is fired in one of the instances and the others are locked, hence increasing the instance count of my application does not increase the number of events fired. </p>
  
  <p><strong>Method A (Timer Interval)</strong></p>
  
  <pre><code>using System;
  using System.Threading.Tasks;
  using System.Timers;
  
  public class Example
  {
      public static void Main()
      {
          var aTimer = new Timer();
          aTimer.Interval = 5000;
          aTimer.Elapsed += OnTimedEventA;
          aTimer.AutoReset = true;
          aTimer.Enabled = true;
  
          var bTimer = new System.Timers.Timer();
          bTimer.Interval = 2000;
          bTimer.Elapsed += OnTimedEventB;
          bTimer.AutoReset = true;
          bTimer.Enabled = true;
  
          Console.WriteLine("Press the Enter key to exit the program at any time... ");
          Console.ReadLine();
      }
      private static void OnTimedEventA(Object source, System.Timers.ElapsedEventArgs e)
      {
          Task.Run(() =&gt;
          {
              Console.WriteLine("The Elapsed event A was raised at {0}", DateTime.Now);
          });
      }
      private static void OnTimedEventB(Object source, System.Timers.ElapsedEventArgs e)
      {
          Task.Run(() =&gt;
          {
              Console.WriteLine("The Elapsed event B was raised at {0}", DateTime.Now);
          });
      }
  }
  </code></pre>
  
  <p><strong>Method B (Task Delay)</strong></p>
  
  <pre><code>using System;
  using System.Threading.Tasks;
  public class Example
  {
      public static void Main()
      {
          EventAAsync();
          EventBAsync();
  
          Console.WriteLine("Press the Enter key to exit the program at any time... ");
          Console.ReadLine();
      }
      private static async Task EventAAsync()
      {
          while (true)
          {
              Task.Run(() =&gt;
              {
                  Console.WriteLine("The Elapsed event A was raised at {0}", DateTime.Now);
              });
              await Task.Delay(TimeSpan.FromSeconds(5));
          }
      }
      private static async Task EventBAsync()
      {
          while (true)
          {
              Task.Run(() =&gt;
              {
                  Console.WriteLine("The Elapsed event B was raised at {0}", DateTime.Now);
              });
              await Task.Delay(TimeSpan.FromSeconds(2));
          }
      }
  }
  </code></pre>
  
  <p>Both Method A and Method B, functionally do the same. 
  They both call fire events A and B at regular intervals of 5s and 2s and forget. Now I'm trying to understand the pros and cons of both approaches. In the end, the events themselves run on separate threads. Theoretically and in practice which of the two should be adopted for handling many such event triggers to be scaled across multiple instances. </p>
  
  <p>Any other methods are also welcome.</p>