title: EF Database context blocks all System.Threading.Timers when callback is static
body: <p>When TimerCallback is static, it will execute only <strong>once</strong> and just hang and will even block any other timers that are running such as timer2. It does reach the end of the method and no exceptions are being thrown. If I make the callback non-static then it works as expected calling the method every x seconds. If I remove the database context then it works fine static or not. The System.Threading.Timer should be queueing calls on a worker thread so even IF the db blocks a thread I don't see how it can block ALL the threads, even the ones that are not accessing the database. Can anyone explain why it is behaving like this?</p>
  
  <pre><code>public class TimerClass
  {
      private System.Threading.Timer timer1;
      private System.Threading.Timer timer2;
  
      public void Start(TimeSpan CheckInterval)
      {
          //timer2 = new Timer(o =&gt; Console.WriteLine("Timer2: " + DateTime.Now.ToString()), null, TimeSpan.Zero, TimeSpan.FromSeconds(1));
          timer1 = new Timer(TimerCallback, null, TimeSpan.Zero, CheckInterval);
      }
  
      private static void TimerCallback(object state)
      {
          Console.WriteLine("Timer1: " + DateTime.Now.ToString());
          using (MyDbEntities db = new MyDbEntities())
          {
              foreach (var item in db.Table)
                  Console.WriteLine(item.Name);
          }
  
          Console.WriteLine("Leaving Callback...");
      }
  }
  </code></pre>
  
  <hr>
  
  <p>This is where the Timer is started. After it is started this Main thread will continue fine but the Timers still hang.</p>
  
  <pre><code>static void Main()
  {
      TimerClass myTimer = new TimerClass();
      myTimer.Start(TimeSpan.FromSeconds(1));
      Console.WriteLine("Timer started...");
      Console.ReadLine(); // pausing here, timers are still in-scope and should be firing
  }
  </code></pre>