COM 互操作 .NET STA
如果我在 .NET 中有一个 STA 线程,并且在该线程中创建了一个 STA COM 对象,然后该线程结束,这会杀死该对象的实例吗?
我的理解是否正确,即 STA COM 对象可以由多个线程访问,并且运行时将自动编组对公寓线程中发生的所有调用?该线程是创建实例的线程吗?那么,如果该线程结束,该实例就会被孤立并丢失?或者是否为 STA 实例创建了单独的线程?
当 ASPCompat=True 时,这在 ASP.Net 中如何发挥作用?我的理解是,每个请求都是由一个随机工作线程处理的,如果我的 STA 组件被放入会话中,它会随机死亡吗,因为创建它的请求处理器线程可能已经完成了?
If I have an STA thread in .NET, and I create an STA COM object in that thread, and then the thread finishes -- does that kill that instance of the object?
Is my understanding correct that STA COM objects can be accessed by multiple threads and runtime will automatically marshal the calls to all happen in the apartment thread? Is that thread the thread that has created the instance? So if that thread finishes, the instance is orphaned and lost? Or is there a separate thread created for STA instances?
How does this play out in ASP.Net with ASPCompat=True? My understanding is that each request is handled by a random worker thread, and if my STA component is placed into the session, will it randomly die because the request processor thread that created it might have finished?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您在 .NET STA 线程上创建 STA COM 对象,则对对象的所有调用都将编组到该线程。
如果您在 .NET MTA 线程上创建 STA COM 对象,运行时将创建一个 STA 线程并编组对该线程的所有调用。
因此,当您的 (STA) 线程存在时,您的 COM 对象将无法访问。
解决方案可能是在新线程上创建对象,您可以控制其生命周期。
我做了类似的事情:
代码启动一个新线程,将公寓设置为 STA 并等待在该线程上创建 COM 对象。
线程本身一直在运行,直到您的应用程序退出 (IsBackground = true) 或您使用 Thread.Abort() 显式终止线程。
但请记住,对 COM 对象的所有调用都经过编组,因此在该线程上依次序列化执行。这可能是您的应用程序的一个大瓶颈。
ASPCompat=true 向 ASP.NET 运行时发出信号,表明您正在使用 STA COM 对象,从而在 STA 线程中运行页面。否则,您可能会遇到异常,或者您的所有 COM 对象将在自动生成的 STA 线程中运行,该线程由对您的页面的所有请求共享(请参阅此处的 MSDN:http://msdn.microsoft.com/en-us/library/zwk9h2kb(VS.80).aspx)
If you create your STA COM object on a .NET STA Thread, all calls to your object are marshalled to that thread.
If you create your STA COM object on a .NET MTA Thread, the runtime will create a STA thread and marshall all calls to that thread.
So, when your (STA) thread exists, your COM objects are inaccessable.
A solution might be to create the objects on a new thread for which you can control the lifetime.
I have done a similar thing like that:
The code starts a new thread, set the appartment to STA and waits for the creation of a COM object on that thread.
The thread itself is running till your application exits (IsBackground = true) or you kill the thread explicitly with Thread.Abort().
But keep in mind, that all calls to your COM objects are marshalled and thus executed serialized one after another on that one thread. That might be a big bottleneck in your App.
ASPCompat=true signals the ASP.NET runtime, that you are using STA COM objects and thus running the page within an STA thread. otherwise you migth get an exception or all your COM objects will run in the automatically generated STA thread shared by all requests to your page (see MSDN here: http://msdn.microsoft.com/en-us/library/zwk9h2kb(VS.80).aspx)