首页 新闻 会员 周边

求助大神——WPF多线程

0
悬赏园豆:50 [已解决问题] 解决于 2012-10-12 09:02

 MyDataSource.Clear();
            Task.Factory.StartNew(() =>
            {                
                GridViewIsBusy = true;            
                var response = _IndoorSampleCheckHttpProxy.GetSamplesIndoorStationsByStationQueryInformation(_StationQueryInformation);
                foreach (var item in response)
                {
                    item.Number = index.ToString();
                    if (item.AjustNumber != null)
                    {
                        if (item.AjustNumber.Contains(item.OldBoxNumber))
                            item.Flag = true;
                    }
                    index++;
                }
                Dispatcher.CurrentDispatcher.Invoke(DispatcherPriority.Send,new Action(delegate()
                {
                    MyDataSource.AddRange(response);                    
                }));            
                GridViewIsBusy = false;
            });

 

 

上面这段代码是在viewmodel层用多线程给gridview加载数据,报错的地方(颜色标记)显示的错误是The calling thread cannot access this object because a different thread owns it。应该怎么办?

super 86的主页 super 86 | 初学一级 | 园豆:11
提问于:2012-10-10 14:28
< >
分享
最佳答案
0

错误的意思是在非UI线程访问了UI控件,把代码中的“Dispatcher.CurrentDispatcher”改成“Application.Current.Dispatcher”应该就可以了。

收获园豆:50
天方 | 大侠五级 |园豆:5407 | 2012-10-10 17:51

牛逼!!

super 86 | 园豆:11 (初学一级) | 2012-10-10 19:53

UI控件是单独的线程控制的吗?

super 86 | 园豆:11 (初学一级) | 2012-10-10 19:54

@super 86: 

WPF线程模型相关知识,可以看下园子里的这篇文章。

http://www.cnblogs.com/zhouyinhui/archive/2008/01/27/1055261.html

天方 | 园豆:5407 (大侠五级) | 2012-10-10 22:29
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册