title: Collection was modified; enumeration operation may not execute
body: <p>I can't get to the bottom of this error, because when the debugger is attached, it does not seem to occur.</p>
  <blockquote>
  <p>Collection was modified; enumeration operation may not execute</p>
  </blockquote>
  <p>Below is the code.</p>
  <p>This is a WCF server in a Windows service. The method <code>NotifySubscribers()</code> is called by the service whenever there is a data event (at random intervals, but not very often - about 800 times per day).</p>
  <p>When a Windows Forms client subscribes, the subscriber ID is added to the subscribers dictionary, and when the client unsubscribes, it is deleted from the dictionary. The error happens when (or after) a client unsubscribes. It appears that the next time the <code>NotifySubscribers()</code> method is called, the <code>foreach()</code> loop fails with the error in the subject line. The method writes the error into the application log as shown in the code below. When a debugger is attached and a client unsubscribes, the code executes fine.</p>
  <p>Do you see a problem with this code? Do I need to make the dictionary thread-safe?</p>
  <pre><code>[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
  public class SubscriptionServer : ISubscriptionServer
  {
      private static IDictionary&lt;Guid, Subscriber&gt; subscribers;
  
      public SubscriptionServer()
      {            
          subscribers = new Dictionary&lt;Guid, Subscriber&gt;();
      }
  
      public void NotifySubscribers(DataRecord sr)
      {
          foreach(Subscriber s in subscribers.Values)
          {
              try
              {
                  s.Callback.SignalData(sr);
              }
              catch (Exception e)
              {
                  DCS.WriteToApplicationLog(e.Message, 
                    System.Diagnostics.EventLogEntryType.Error);
  
                  UnsubscribeEvent(s.ClientId);
              }
          }
      }
      
      public Guid SubscribeEvent(string clientDescription)
      {
          Subscriber subscriber = new Subscriber();
          subscriber.Callback = OperationContext.Current.
                  GetCallbackChannel&lt;IDCSCallback&gt;();
  
          subscribers.Add(subscriber.ClientId, subscriber);
          
          return subscriber.ClientId;
      }
  
      public void UnsubscribeEvent(Guid clientId)
      {
          try
          {
              subscribers.Remove(clientId);
          }
          catch(Exception e)
          {
              System.Diagnostics.Debug.WriteLine(&quot;Unsubscribe Error &quot; + 
                      e.Message);
          }
      }
  }
  </code></pre>