COM Interop .NET STA [英] COM Interop .NET STA

查看:75
本文介绍了COM Interop .NET STA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在.NET中有一个STA线程,并且在该线程中创建了一个STA COM对象,然后该线程完成,那会杀死该对象的实例吗?

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?

我的理解是否正确,STA COM对象可以被多个线程访问,运行时会自动封送对所有发生在单元线程中的调用?该线程是创建实例的线程吗?那么,如果该线程完成,实例将成为孤立对象并丢失?还是有为STA实例创建的单独线程?

如何在ASP.Net中使用ASPCompat = True进行播放?我的理解是,每个请求都由一个随机的工作线程处理,如果将我的STA组件放入会话中,它会随机死掉,因为创建它的请求处理器线程可能已经完成了吗?

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?

推荐答案

如果在.NET STA线程上创建STA COM对象,则对该对象的所有调用都将编组到该线程。

If you create your STA COM object on a .NET STA Thread, all calls to your object are marshalled to that thread.

如果在.NET MTA线程上创建STA COM对象,则运行时将创建STA线程并封送对该线程的所有调用。

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.

因此,当您的(STA)线程存在时,您的COM对象将无法访问。

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.

我做了类似的事情:

using (ManualResetEventSlim mre = new ManualResetEventSlim(false))
{  
    Thread _STAThread = new Thread(new ThreadStart(() =>
                {
                    globalComObject = new ComClass();
                    mre.Set();
                    try
                    {
                        Thread.CurrentThread.Join();
                    }
                    catch (ThreadAbortException)
                    {
                    }
                }));
                _STAThread.SetApartmentState(ApartmentState.STA);
                _STAThread.IsBackground = true;
                _STAThread.Start();
                mre.Wait();
}

代码启动一个新线程,将单元设置为STA并等待在该线程上创建一个COM对象。
线程本身一直运行到您的应用程序退出(IsBackground = true)或使用Thread.Abort()明确杀死该线程。

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().

但请记住,所有对您COM对象的调用都会被整理,从而在一个线程上一个接一个地序列化执行。这可能是应用程序中的一个大瓶颈。

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表示ASP.NET运行时,您正在使用STA COM对象,从而在STA中运行页面线。否则,您将获得异常,否则所有COM对象将在由 all 请求共享给您的页面的自动生成的STA线程中运行(请参见MSDN,网址为: http://msdn.microsoft.com/en-us/library/zwk9h2kb(VS.80).aspx

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)

这篇关于COM Interop .NET STA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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