此功能可更新活动用户列表。删除那些不在服务器列表中的人。添加了不在客户列表中的人。该函数是从不同的线程调用的,但为了避免混淆,我将所有内容都放在处理队列Dispatcher
中//This function is called from different threads
public void UpdateUsers(List<ServerUser> serverUsers)
{
//imitation one thread mode
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
//remove old user
ClientUsers.ToList().ForEach(clientUser =>
{
if (serverUsers.All(serverUser => clientUser.Id != serverUser.Id))
{
OnUserEvent(clientUser, false);
ClientUsers.Remove(clientUser);
}
});
serverUsers.ForEach(serverUser =>
{
var clientUser = ClientUsers.SingleOrDefault(clientUser => serverUser.Id == clientUser.Id);
//Add new user
if (clientUser == null)
{
clientUser = new ClientUser(serverUser);
ClientUsers.Add(clientUser);
}
});
}), DispatcherPriority.ApplicationIdle).Wait();//Wait completion!!!
}
结果,我明白了
System.InvalidOperationException:Sequence在System.Linq.Enumerable.SingleOrDefault [TSource](IEnumerable 1 source, Func
2谓词)中包含多个匹配元素
我哪里错了?