本文介绍了调用线程无法访问此对象,因为另一个线程拥有它。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public void LoadData()
{
var manager = new ManageFactory& lt; ISmartDealerManager& gt;()。GetManager();
List& lt; DealerItem& gt; DealerCollection = manager.LoadDealerData();
MainDataGrid.Items.Clear();
MainDataGrid.ItemsSource = null ;
Thread.Sleep( 2000 );
if (DealerCollection.Count()& gt; 0 )
{
MainDataGrid.Items.Clear();
this .Dispatcher.BeginInvoke( new Action(()=& gt;
{
MainDataGrid.ItemsSource = DealerCollection;
lblError.Visibility = Visibility.Hidden;
}),DispatcherPriority.Background);
}
else
{
MainDataGrid.ItemsSource = 空跨度>;
lblError.Visibility = Visibility.Visible;
}
EnableDisableItems();
this .WaitingScreen.IsLoadingVisible = true ;
}
}
线程MyNewThread = 新线程( new ThreadStart(()=& gt; LoadData()));
MyNewThread.IsBackground = true ;
MyNewThread.Start();
这里我在MainDataGrid.Items.Clear();行收到错误
和想法请。
解决方案
public void LoadData() { var manager = new ManageFactory<ISmartDealerManager>().GetManager(); List<DealerItem> DealerCollection = manager.LoadDealerData(); MainDataGrid.Items.Clear(); MainDataGrid.ItemsSource = null; Thread.Sleep(2000); if (DealerCollection.Count() > 0) { MainDataGrid.Items.Clear(); this.Dispatcher.BeginInvoke(new Action(() => { MainDataGrid.ItemsSource = DealerCollection; lblError.Visibility = Visibility.Hidden; }), DispatcherPriority.Background); } else { MainDataGrid.ItemsSource = null; lblError.Visibility = Visibility.Visible; } EnableDisableItems(); this.WaitingScreen.IsLoadingVisible = true; } } Thread MyNewThread = new Thread(new ThreadStart(() => LoadData())); MyNewThread.IsBackground = true; MyNewThread.Start();
Here i am getting error at line " MainDataGrid.Items.Clear();"
and idea please.
解决方案
这篇关于调用线程无法访问此对象,因为另一个线程拥有它。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!