为什么某些对象不能从不同的线程访问? [英] Why are some objects not accessible from different threads?

查看:84
本文介绍了为什么某些对象不能从不同的线程访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在用C#开发时,我多次遇到这个问题.我会很开心地编码,在线程之间来回传递对象,而在其他线程之间来回传递,然后突然间我得到了这个熟悉的错误:

I've come across this problem several times while developing in C#. I'll be a happily coding along, passing objects to and fro between threads and what not, then all of a sudden I get this familiar error:

调用线程无法访问该对象,因为不同 线程拥有它."

"The calling thread cannot access this object because a different thread owns it."

好吧,好吧,我之前已经处理过它,尤其是在GUI线程上的对象上.您只需要编写一些额外的代码来解决该特定问题.但是,每当我遇到一个绝对是普通的对象时,却不喜欢被不同的线程访问.

Well, ok, I've dealt with it before, especially with objects on the GUI thread. You just have to write some extra code to program around that specific problem. But every once in while I come across an object that is by all means ordinary, yet it doesn't like being accessed by different threads.

编辑我在原始帖子中误解了导致访问异常的对象.不是 IP地址 ,而不是其 System.Printing.PrintQueue. ,该地址是我用来获取IP地址的.这是您无法通过多个线程评估的对象.

EDIT I was mistaken in my original post about the object that was causing the access exception. It was NOT IPAddress, instead its System.Printing.PrintQueue. which I was using to obtain the IP address. This is the object that you can't assess from more than 1 thread.

我写的所有课程都从来没有这个问题.我什至不知道自己该如何实现.您是否必须保留一个具有创建您的线程ID的成员变量,然后在每个属性和方法访问中对照该线程检查当前线程?好像疯了.微软为什么要决定....好吧... PrintQueue,绝对不能在线程间共享.但是其他类....很好."

All my classes I've written never have this problem. I don't even know how I'd implement this myself. Would you have to keep a member variable with the thread ID that created you, and then check the current thread against that on every single property and method access? That seems crazy. Why would Microsoft decide that..... "OK... PrintQueue, definitely not sharable among threads. But these other classes.... their good to go."

为什么某些对象被阻止进行多线程访问?

推荐答案

我认为这可能解释得很好,我认为这与COM有关.

I think this may explain things fairly well, I think this specifically has to do with COM.

http://msdn.microsoft.com/zh-cn/library/ms693344%28v = vs.85%29

具体地.

通常,查看COM线程体系结构的最简单方法是 将过程中的所有COM对象都分为几组 叫做公寓.一个COM对象恰好住在一个公寓里, 感觉到它的方法只能由线程合法地直接调用 属于那个公寓.任何其他想要调用的线程 该对象必须通过代理.

In general, the simplest way to view the COM threading architecture is to think of all the COM objects in the process as divided into groups called apartments. A COM object lives in exactly one apartment, in the sense that its methods can legally be directly called only by a thread that belongs to that apartment. Any other thread that wants to call the object must go through a proxy.

有两种类型的套间:单线程套间和 多线程单元.

There are two types of apartments: single-threaded apartments, and multithreaded apartments.

单线程单元仅由一个线程组成,因此生活在单线程单元中的所有COM对象都可以接收 方法仅从属于该单元的一个线程进行调用. 在单线程单元中对COM对象的所有方法调用都是 与Windows消息队列同步的单线程 公寓的线程.具有单执行线程的进程是 只是该模型的一个特例.

Single-threaded apartments consist of exactly one thread, so all COM objects that live in a single-threaded apartment can receive method calls only from the one thread that belongs to that apartment. All method calls to a COM object in a single-threaded apartment are synchronized with the windows message queue for the single-threaded apartment's thread. A process with a single thread of execution is simply a special case of this model.

多线程单元由一个或多个线程组成,因此生活在多线程单元中的所有COM对象都可以接收方法 直接从属于该线程的任何线程中调用 多线程的公寓.多线程单元中的线程使用 该模型称为自由线程.调用多线程中的COM对象 公寓由对象本身同步.

Multithreaded apartments consist of one or more threads, so all COM objects that live in an multithreaded apartment can receive method calls directly from any of the threads that belong to the multithreaded apartment. Threads in a multithreaded apartment use a model called free-threading. Calls to COM objects in a multithreaded apartment are synchronized by the objects themselves.

这篇关于为什么某些对象不能从不同的线程访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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