我有以下代码可以在用户离开时修改可观察的“屏幕”集合。
void OnUserLeft(int roomId, int userId, string username)
{
client.ClientDispatcher.Invoke(
(Action<int>)((id) =>
{
Console.WriteLine("Hello before the storm!");
var screensToCheck = client.Screens.Where(s => s.CpuId == id).ToList();
screensToCheck.Each(s => client.Screens.Remove(s));
Console.WriteLine("Hello there!");
}), userId);
}
这包含在对客户端 Dispatcher 的调用中,据说是为了解决与 CollectionViews 相关的线程问题。但是,我仍然得到以下异常:
这种类型的 CollectionView 不支持从不同于 Dispatcher 线程的线程更改其 SourceCollection。
您在上面看到的 Dispatcher 是在 WPF 应用程序的 MainViewModel 中设置的(我们使用的是 MVVM),如下所示:
public Dispatcher ClientDispatcher
{
get { return Dispatcher.CurrentDispatcher; }
}