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。应该怎么办?
错误的意思是在非UI线程访问了UI控件,把代码中的“Dispatcher.CurrentDispatcher”改成“Application.Current.Dispatcher”应该就可以了。
牛逼!!
UI控件是单独的线程控制的吗?
@super 86:
WPF线程模型相关知识,可以看下园子里的这篇文章。
http://www.cnblogs.com/zhouyinhui/archive/2008/01/27/1055261.html