正在此时需要STA消息循环? [英] Is STA Message Loop Required in This Case?

查看:107
本文介绍了正在此时需要STA消息循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我创建并在.NET应用程序的线程运行某些COM对象。该线程被标记为单线程公寓,一切似乎是工作。我的理解是,如果这些线程试图从主线程访问COM对象,然后这些对象会被自动编组,并在.NET系列化我,所以即使在这种情况下,事情会为我处理,所有的安全和整洁,但也许有点慢。

I've got some COM objects I'm creating and running on threads in a .NET application. The threads are marked as Single Threaded Apartments, and everything seems to be working. My understanding is that if these threads try to access COM objects from the main thread then those objects will automatically be marshaled and serialized for me in .NET, so even in that case things will be handled for me, all safe and neat, though maybe a bit slowly.

我的问题是,尽管事情似乎是工作的罚款,我的不可以在STA线程我创建抽消息循环。我宁愿避免消息循环,如果我可以,因为额外的并发症,会引起(和可能的效率损失以及)。

My question is, while things appear to be working fine, I am not pumping a message loop in the STA threads I'm creating. I'd rather avoid the message loop if I can because of the extra complications it would cause (and possible efficiency losses as well).

我读过的原因的消息循环是必要的(主要来自非常有帮助汉斯帕桑特)一堆意见,我的理解是,消息循环给出了一个线程A,其中一些其他的线程B可以要求地方住在线程A的COM对象可以编组,并打了。如果这是正确的,那么只要没有其他线程的线程A的COM对象要求什么,是线程A安全的不抽一个消息循环?还是有其他的情况下,该消息循环可能会发挥作用呢?

I've read a bunch of advice on why the message loop is necessary (mostly from the very helpful Hans Passant), and my understanding is that the message loop gives a place on a thread A where some other thread B can request that COM objects living on thread A can be marshaled and played with. If that's correct, then as long as no other threads request anything from the COM objects on thread A, is Thread A safe in not pumping a message loop? Or are there other cases where the message loop might come into play as well?

我是不是在玩火吗?而且是有过,你问,如果你在玩火的情况下,你是不是?

Am I playing with fire here? And is there ever a case where you ask if you're playing with fire and you're not?

推荐答案

在STA合同要求抽消息循环。但是,是的,有可能逃脱不抽。有可能出错的地方两个主要方面:

The STA contract requires pumping a message loop. But yes, it is possible to get away with not pumping. There are two major things that can go wrong:

  • 这年代提出的接口方法从另一个公寓,其中包括另一个STA线程或线程在MTA将无法完成任何呼叫。这看起来像僵局程序,调用根本不会返回。要注意的是,你可以控制你自己的呼叫相当不错,但你不知道什么是COM组件在做什么。它可能启动一个线程本身。你可以看到这与调试+的Windows +线程调试器。请确保您正在运行在非托管模式下的调试器,你可以占到所有你看到的线程。不是特别容易BTW。

  • Any call that's made on an interface method from another apartment, including another STA thread or a thread in the MTA will not complete. This looks like deadlock in your program, the call simply never returns. Beware that you can control your own calls quite well but you don't know what the COM component is doing. It may well start a thread itself. You can see this in the debugger with Debug + Windows + Threads. Make sure you are running the debugger in unmanaged mode and that you can account for all the threads you see. Not particularly easy btw.

许多单元线程COM组件指望有一个消息循环,以自己的需要照顾。这可能是一些无关痛痒的一个计时器时,有没有循环,也不会打勾。或者,它可能会做内部编组。用狙++,检查是否存在由新的STA线程,确保风吹草动如果你看到一家公司拥有的任何隐藏的窗口。诊断仅仅是行为不端的组件。不认识活动是一种常见的事故。

Many apartment threaded COM components count on having a message loop taking care of their own needs. It could be something innocuous as a Timer, it won't tick when there's no loop. Or it may do marshaling internally. Use Spy++ and check if there are any hidden windows owned by your new STA thread, sure sign of trouble if you see one. The diagnosis is the component just misbehaving. Not raising events is a common mishap.

真的没什么钉在墙上,当你不知道有足够的了解服务器的内部结构。一定要测试赫克出来。

Nothing really to nail to a wall when you don't know enough about the internals of the server. Be sure to test the heck out of it.

这篇关于正在此时需要STA消息循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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