简体   繁体   English

方法不返回控制

[英]Method doesn't return controlling

I have a grid on WPF form and another class, that has some events. 我在WPF表单和另一个类上都有一个网格,其中包含一些事件。 From my wpf form i subscribe on those events and i want them to add some objects to my grid, but only that i have is "The calling thread cannot access this object because a different thread owns it." 我从wpf表单中订阅了这些事件,并希望它们将一些对象添加到网格中,但唯一的问题是“调用线程无法访问此对象,因为其他线程拥有它。” How can I avoid this proble and get same functionality? 如何避免这个问题并获得相同的功能?

This has been covered ad nauseam on StackOverflow and elsewhere. 这已在StackOverflow和其他地方的ad nauseam中涵盖。 You need to use the Dispatcher to marshal your access back to the UI thread. 您需要使用Dispatcher来整理对UI线程的访问。 For example: 例如:

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;
    });
}

This is a cross-threading issue. 这是一个跨线程问题。 Look into delegate creation so you can safely invoke another thread to modify something that was created on the different thread. 查看委托创建,以便您可以安全地调用另一个线程来修改在另一个线程上创建的内容。 Here is a good MSDN article about how to make these thread-safe calls. 这是一篇有关如何进行这些线程安全的调用的MSDN优秀文章。

http://msdn.microsoft.com/en-us/library/ms171728(v=vs.80).aspx http://msdn.microsoft.com/zh-cn/library/ms171728(v=vs.80).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM