在进行WPF开发过程中,需要从一个新的线程中操作ObservableCollection,结果程序抛出一个NotSupportedException的错误:

This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread

看其字面意思是跨线程操作不被支持。

下面的代码展示了这种错误出现的根源:

  ObservableCollection<UserListViewModel> users = new ObservableCollection<UserListViewModel>();
public ObservableCollection<UserListViewModel> Users
{
get { return users; }
set { users = value; }
}
        /// <summary>
/// 开启监听线程
/// </summary>
private void openListeningThread()
{
isRun = true;
Thread t = new Thread(new ThreadStart(() =>
{
IPEndPoint ipEnd = new IPEndPoint(broadIPAddress, lanPort);
try
{
while (isRun)
{
try
{
byte[] recInfo = listenClient.Receive(ref ipEnd); //接受内容,存储到byte数组中
DealWithAcceptedInfo(recInfo); //处理接收到的数据
}
catch (Exception ex) { MessageBox.Show(ex.Message); }
} listenClient.Close(); isRun = false;
}
catch (SocketException se) { throw new SocketException(); } //捕捉试图访问套接字时发生错误。
catch (ObjectDisposedException oe) { throw new ObjectDisposedException(oe.Message); } //捕捉Socket 已关闭
catch (InvalidOperationException pe) { throw new InvalidOperationException(pe.Message); } //捕捉试图不使用 Blocking 属性更改阻止模式。
catch (Exception ex) { throw new Exception(ex.Message); }
})); t.Start();
} /// <summary>
/// 方法:处理接到的数据
/// </summary>
private void DealWithAcceptedInfo(byte[] recData)
{
BinaryFormatter formatter = new BinaryFormatter();
MessageFlag recvMessageFlag; MemoryStream ms = new MemoryStream(recData);
try { recvMessageFlag = (MessageFlag)formatter.Deserialize(ms); }
catch (SerializationException e) { throw; } UserListViewModel uListViewModel = new UserListViewModel(new UserDetailModel { MyIP = recvMessageFlag.UserIP, MyName = recvMessageFlag.UserName }); switch (recvMessageFlag.Flag)
{
case "0x00":
//这里很关键,当检测到一个新的用户上线,那么我们需要给这个新用户发送自己的机器消息,以便新用户能够自动添加进列表中。
SendInfoOnline(recvMessageFlag.UserIP); if (!list.Contains(uListViewModel.MyInfo))
{
list.Add(uListViewModel.MyInfo);
Users.Add(uListViewModel);
} break;
case "0x01":
//AddTextBox(, int titleOrContentFlag, int selfOrOthersFlag);
//AddTextBox(string info, int titleOrContentFlag, int selfOrOthersFlag);
break;
case "0x02": break;
case "0x03":
if (list.Contains(uListViewModel.MyInfo))
{
// list.Remove(uListViewModel.MyInfo);
Users.Remove(uListViewModel);
}
break;
default: break;
}
}

上面的方法如果在一个新的Thread中创建,就将会产生这种问题。

解决方法如下:

public class AsyncObservableCollection<T> : ObservableCollection<T>
{
//获取当前线程的SynchronizationContext对象
private SynchronizationContext _synchronizationContext = SynchronizationContext.Current;
public AsyncObservableCollection() { }
public AsyncObservableCollection(IEnumerable<T> list) : base(list) { }
protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{ if (SynchronizationContext.Current == _synchronizationContext)
{
//如果操作发生在同一个线程中,不需要进行跨线程执行
RaiseCollectionChanged(e);
}
else
{
//如果不是发生在同一个线程中
//准确说来,这里是在一个非UI线程中,需要进行UI的更新所进行的操作
_synchronizationContext.Post(RaiseCollectionChanged, e);
}
}
private void RaiseCollectionChanged(object param)
{
// 执行
base.OnCollectionChanged((NotifyCollectionChangedEventArgs)param);
}
protected override void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (SynchronizationContext.Current == _synchronizationContext)
{
// Execute the PropertyChanged event on the current thread
RaisePropertyChanged(e);
}
else
{
// Post the PropertyChanged event on the creator thread
_synchronizationContext.Post(RaisePropertyChanged, e);
}
}
private void RaisePropertyChanged(object param)
{
// We are in the creator thread, call the base implementation directly
base.OnPropertyChanged((PropertyChangedEventArgs)param);
}
}

将上面的ObservableCollection替换掉即可。

        AsyncObservableCollection<UserListViewModel> users = new AsyncObservableCollection<UserListViewModel>();
public AsyncObservableCollection<UserListViewModel> Users
{
get { return users; }
set { users = value; }
}

参考文章:[WPF] Binding to an asynchronous collection

之所以利用SynchronizationContext,我觉得是因为后台处理线程和UI进行交互的时候,没有获取到SynchronizationContext的状态导致的。因为后台和前台的线程交互,需要通过SynchronizationContext的Send或者Post方法才能避免线程Exception。

有人说可以利用Control.Invoke方法来实现啊。。。实现个鬼啊,这里就没有Control.....你只能自己来同步SynchronizationContext了。

参考文章来源:http://www.cnblogs.com/scy251147/archive/2012/10/30/2745760.html

最新文章

  1. Qt-为应用程序添加logo
  2. ios 弹出不同的键盘
  3. UIScrollView的代理(delegate)
  4. ActionBar compat 如何禁用ActionBar的显示/隐藏动画
  5. 设置时间&amp;时区
  6. Asp.net Mvc 使用EF6 code first 方式连接MySQL总结
  7. waterMarkTextBox
  8. android studio 中的编码问题
  9. C++引用(References)
  10. Ubuntu 14.10 下安装Synergy,不同电脑之间公用一套键盘鼠标
  11. Polygon Table - Google Chrome
  12. Eclipse怎样导入github上的项目
  13. jquery 滚动加载
  14. Nginx 之二: nginx.conf 配置及基本优化
  15. python报错“UnicodeEncodeError: &#39;ascii&#39; codec can&#39;t encode characters in position 22-26: ordinal not in range(128)”问题解决
  16. linux系统下phpstudy里的mysql使用方法
  17. python 决策树
  18. Java基础知识➣多线程编程(五)
  19. MVEAN_day05 Nexus私服对的搭建
  20. day 13

热门文章

  1. 学无止境,我爱python
  2. 总结加密、机密jar中的class
  3. jQuery WeUI
  4. IDEA 安装lombok及使用
  5. 《Python之BMI计算》
  6. UMP系统功能 分库分表
  7. LuoguP3338 [ZJOI2014]力
  8. win 10安装Nginx,php,mysql 小计
  9. UVA - 374
  10. LoadRunner例子:检查点为参数的一个例子