title: C# thread and exception "Sequence contains more than one matching element"
body: <p>This feature updates the list of active users. Remove those who are not in the server list. Added those who are not in the client list. The function is called from different threads, but to avoid confusion I put everything in the processing queue Dispatcher   </p>
  
  <pre><code>//This function is called from different threads
          public void UpdateUsers(List&lt;ServerUser&gt; serverUsers)
          {
              //imitation one thread mode 
              Application.Current.Dispatcher.BeginInvoke(new Action(() =&gt;
              {
                  //remove old user
                  ClientUsers.ToList().ForEach(clientUser =&gt;
                  {
                      if (serverUsers.All(serverUser =&gt; clientUser.Id != serverUser.Id))
                      {
                          OnUserEvent(clientUser, false);
                          ClientUsers.Remove(clientUser);
                      }
                  });
  
                  serverUsers.ForEach(serverUser =&gt;
                  {
                      var clientUser = ClientUsers.SingleOrDefault(clientUser =&gt; serverUser.Id == clientUser.Id);
  
                      //Add new user
                      if (clientUser == null)
                      {
                          clientUser = new ClientUser(serverUser);
                          ClientUsers.Add(clientUser);
                      }
                  });
  
              }), DispatcherPriority.ApplicationIdle).Wait();//Wait completion!!!
          }
  </code></pre>
  
  <p>As a result, I get it
  System.InvalidOperationException: Sequence contains more than one matching element at System.Linq.Enumerable.SingleOrDefault [TSource] (IEnumerable<code>1 source, Func</code>2 predicate)</p>
  
  <p><strong>Where am I wrong?</strong></p>