2011-10-06 95 views
1

我有一个WPF窗体和另一个类,有一些事件的网格。从我的wpf表单我订阅这些事件,我希望他们添加一些对象到我的网格,但只有我的是“调用线程不能访问此对象,因为不同的线程拥有它。”我怎样才能避免这个问题,并获得相同的功能?方法不返回控制

+0

+1点,让您可以发表评论 – Aaron

+0

这是一个重复的问题:[点击这里](http://stackoverflow.com/questions/3146942/the-calling-thread-cannot-access-this -object-because-a-different-thread-owns-it)或[here](http://stackoverflow.com/questions/5996162/the-calling-thread-cannot-access-this-object-because-a-不同线程拥有它)或约八个人:) – Kevek

回答

1

这已经在StackOverflow和别处讨论过了。您需要使用Dispatcher将您的访问编组回到UI线程。例如:

private void OnSomeEvent(object sender, EventArgs e) 
{ 
    // this is being called on a thread other than the UI thread so marshal back to the UI thread 
    Dispatcher.BeginInvoke((ThreadStart)delegate 
    { 
     // now the grid can be accessed 
     grid.Whatever = foo; 
    }); 
}