调用线程无法访问该对象,因为其他线程拥有该对象 [英] The calling thread cannot access thi object because a different thread owns it

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

问题描述

朋友,

我在WPF中设计了一个小工具,其中包含两种形式.第一种形式在按钮单击事件中执行巨大的任务.由于此过程,UI无响应.为了解决这个问题,我采用了线程概念.

问题:完成这项艰巨的任务后,我要调用Form2,它包含一个"DocumentViewer".由于Form2是通过线程从Form1调用的,因此默认打印不起作用.

错误消息:调用线程无法访问该对象,因为其他线程拥有它"

请提出一些解决方法,以避免出现此问题.

预先感谢,
问候,
Suresh Kumar.B

Hi Friends,

I have designed a small tool in WPF, which contains two forms. The first form performs a huge task in a button click event. Due to this process, the UI is non-responsive. To over come this issue, I have gone for Threading concept.

Problem: After performing this huge task, I am calling Form2, which contains a "DocumentViewer". Since, the Form2 is called from the Form1 through a thread, the Default Printing is not working.

Error Message: "The calling thread cannot access thi object because a different thread owns it"

Please suggest some workaround to avoid this issue.

Thanks in advance,
Regards,
Suresh Kumar.B

推荐答案

那是因为UI线程拥有UI元素.

That''s because the UI thread owns the UI elements.

为了从另一个线程升级UI,可以使用调度程序,如此链接中所述: http://www.switchonthecode.com /tutorials/working-with-the-wpf-dispatcher [ ^ ].

In order to upadte the UI from another thread, you can use the Dispatcher, as explained in this link: http://www.switchonthecode.com/tutorials/working-with-the-wpf-dispatcher[^].


所有WPF对象实际上都是依赖对象,因此只能在原来的线程中将它们弄乱.创建于(因为是拥有它们的线程).您需要做的是将任务的逻辑与UI分开.这意味着您无法从后台线程访问任何依赖项属性.我通常要做的是,获取我感兴趣的依赖项属性的值,并在我第一次启动该过程时将其以对象数组的形式传递给后台线程.

话虽这么说,如果您正在使用ListBox(或类似的东西)中的对象集合,那么您可能需要重新考虑自己的UI方式.这就是绑定的目的.您将非DependencyObjects的集合放置在非DependencyProperty中保存的ObservableCollection中.然后,将类似于ListBox的对象的ItemsSource属性绑定到该集合.这样,您将能够从您的收藏夹中读取内容.但是,要插入到集合中,您将必须在拥有线程中执行此操作(对集合所做的更改将通过绑定来更新ListBox,只允许您从ListBox的拥有线程中执行此操作).有多种方法可以使用拥有的线程分派器将其分解为较小的组件,以防止线程冻结.
All WPF objects are really dependency objects, so they they can only be messed with in the thread that they were created in (because that''s the thread that owns them). What you need to do is separate the logic of the task form the UI. This means that you cannot access any dependency properties from your background thread. What I usually do, is grab the values of the dependency properties I''m interested in, and pass them on in an object array to the background thread when I first start the process.

That being said, if you are working with a collection of objects in a ListBox (or something like that) than you may want to re-think how you''re doing your UI. This is what binding is for. You place your collection of non-DependencyObjects in an ObservableCollection that is being held in a non-DependencyProperty. Then you bind the ItemsSource property of you ListBox like object to that collection. This way, you will be able to read from your collection. Yet, to insert into the collection, you will have to do that from the owning thread (changes to the collection will update the ListBox through the binding, which you''re only allowed to do from the ListBox''s owning thread). There are ways to use the owning threads Dispatcher to break that down into smaller components to keep the thread from freezing.


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

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