调用线程无法访问此对象,因为另一个线程拥有它。 [英] The calling thread cannot access this object because a different thread owns it.

查看:507
本文介绍了调用线程无法访问此对象,因为另一个线程拥有它。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  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();行收到错误



和想法请。

解决方案

嗨拉曼,



答案很简单,你试图清除UI线程之外的MainDataGrid.Items。



你应该改变你的代码:



  if (DealerCollection.Count()>  =  0 
{

this .Dispatcher.BeginInvoke( new Action( ()= >
{
MainDataGrid.Items.Clear();
MainDataGrid.ItemsSource = DealerCollection;
lblError.Visibility = Visibility.Hidden;
}),DispatcherPriority.Background);

} < / pre >


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 = null;
               lblError.Visibility = Visibility.Visible;
           }
           EnableDisableItems();
           this.WaitingScreen.IsLoadingVisible = true;
       }
   }

Thread MyNewThread = new Thread(new ThreadStart(() =&gt; LoadData()));
            MyNewThread.IsBackground = true;
            MyNewThread.Start();





Here i am getting error at line " MainDataGrid.Items.Clear();"

and idea please.

解决方案

Hi Raman,

The answer is very simple, you are trying to clear the MainDataGrid.Items outside of the UI thread.

You should change your code:

if (DealerCollection.Count() >= 0)
{
  
    this.Dispatcher.BeginInvoke(new Action(() =>
    {
        MainDataGrid.Items.Clear();
        MainDataGrid.ItemsSource = DealerCollection;
        lblError.Visibility = Visibility.Hidden;
    }), DispatcherPriority.Background);

}</pre>


这篇关于调用线程无法访问此对象,因为另一个线程拥有它。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆