title: contextswitchdeadlock
body: <p>Whilst debugging my program in VS 2008 I have come across the following error:</p>
  
  <p>The CLR has been unable to transition from COM context 0x34fc1a0 to COM context 0x34fc258 for 60 seconds. The thread that owns the destination context/apartment is most likely either doing a non pumping wait or processing a very long running operation without pumping Windows messages. This situation generally has a negative performance impact and may even lead to the application becoming non responsive or memory usage accumulating continually over time. To avoid this </p>
  
  <p>It appears to be deadlocking even though the code only contains a simple C# timer: See Snippet Below:</p>
  
  <pre><code>    private void RequestWork()
      {
          // The timer will be re-intialised if there are still no wating jobs in the database
          StopTimer();
  
          // assign all the threads some work
          InitialiseTimer();
  
      }
  
  
  
      /// &lt;summary&gt;
      /// Initialise a timer with a timer interval configured from app.config. Enable the timer and 
      /// register an appropriate event handler
      /// &lt;/summary&gt;
      private void InitialiseTimer()
      {
  
  
          if (m_Timer == null)
          {
              // look up the default backoff time from the config
              string backOffInt = ConfigurationSettings.AppSettings["BackOffInterval"];
  
              int backoffInterval = 1000;
  
  
              m_Timer = new System.Timers.Timer();
  
  
              // set the timer interval to 5 seconds
              m_Timer.Interval = backoffInterval;
  
              m_Timer.Elapsed += new ElapsedEventHandler(m_Timer_Elapsed);
          }
  
          m_Timer.Enabled = true;
      }
  
  
      private void StopTimer()
      {
  
          if (m_Timer != null)
          {
              m_Timer.Enabled = false;
          }
      }
  
      void m_Timer_Elapsed(object p_Sender, ElapsedEventArgs p_E)
      {
  
          RequestWork();
      }
  </code></pre>
  
  <p>As far as I know the timer should run, elapse and then initialise again, I can see no local reason for a deadlock.</p>
  
  <p>I am aware of how to turn this error msg off but feel that this is not a solution, instead it is masking the problem.</p>