解决DisconnectedContext在Visual StudioDisconnectedContext、Visual、Studio

2023-09-03 16:30:07 作者:谈不起的一场恋爱

我总是一个DisconnectedContext(一个托管调试助手),当我使用Visual Studio中运行我的应用程序。鉴于谷歌和文档,当STA COM对象从其他线程调用会发生这种情况。

I always got a DisconnectedContext (a managed debugging assistant) when I run my application using Visual Studio. Given Google and docs, this can happen when COM objects on STA are called from other thread.

然而,当我看到throught所有线程的弹出窗口的时候,我不觉得这样的事。 (我也没有发现任何奇怪的话)。

However, when I look throught all the threads when the popup appears, I don't find anything like this. (And I don't find anything weird at all).

我如何能找到DisconnectedContext提高的方式有些想法?

Some ideas on how I can find the way the DisconnectedContext is raised ?

推荐答案

发现这个在寻找相同的答案,以为我会添加评论...

Found this while looking for the same answer, thought I'd add a comment...

此错误是在任何多线程应用程序使用CLR几乎不可避免的过程中的互操作对象通过(瞬态线程)。的问题是,在CLR有对象的非确定性的清理(其可以是RCW的,与基础COM对象线程关联性)。有没有办法,你可以告诉运行时清理在线程创建的对象(至少在没有创建线程上的另一个不确定性净化处理);它的互操作机制的设计限制。鉴于此,有没有办法永远安全地退出已创建的任何CLR对象线程没有可能收到此错误。

This error is virtually unavoidable in any multi-threaded app using CLR objects through in-process interop (on transient threads). The problem is that the CLR had non-deterministic cleanup of objects (which may be RCW's, with thread-affinity on the underlying COM objects). There's no way you can tell the runtime to clean up objects created on a thread (at least without creating another non-deterministic cleanup handle on the thread); it's a design limitation of the interop mechanism. Given that, there's no way to ever safely exit a thread which has created any CLR objects without potentially getting this error.

最好的建议:不要使用CLR /互操作的,如果你能帮助它。接下来最好的建议:使用COM +来处理,分离出的互操作,使CLR能够生活在一个过程,永远不会终止线程(使用持久线程池或同等学历)。接下来最好的建议:我一起继续跟微软与他们的互操作这一设计层面的问题,并希望他们能解决这个问题。

Best advice: don't use CLR/interop if you can help it. Next best advice: use COM+ to process-isolate your interop, so the CLR can live in a process which never terminates threads (use persistent thread pool or equivalent). Next best advice: join me in continuing to tell Microsoft about this design-level problem with their interop, and hope they fix it.