简体   繁体   English

COM Interop .NET STA

[英]COM Interop .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? 如果我在.NET中有一个STA线程,并且在该线程中创建了一个STA COM对象,然后该线程完成了,那会杀死该对象的实例吗?

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? 我的理解正确吗,STA COM对象可以由多个线程访问,运行时将自动封送对所有发生在单元线程中的调用? 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? 还是为STA实例创建了单独的线程?
How does this play out in ASP.Net with ASPCompat=True? 如何在ASP.Net中使用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? 我的理解是,每个请求都由一个随机的工作线程处理,如果将我的STA组件放入会话中,它会随机死掉,因为创建它的请求处理器线程可能已经完成了吗?

If you create your STA COM object on a .NET STA Thread, all calls to your object are marshalled to that thread. 如果在.NET STA线程上创建STA COM对象,则对对象的所有调用都将编组到该线程。

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. 如果在.NET MTA线程上创建STA COM对象,则运行时将创建STA线程并编组对该线程的所有调用。

So, when your (STA) thread exists, your COM objects are inaccessable. 因此,当您的(STA)线程存在时,将无法访问您的COM对象。

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: 我做了类似的事情:

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();
}

The code starts a new thread, set the appartment to STA and waits for the creation of a COM object on that thread. 该代码启动一个新线程,将单元设置为STA,并等待在该线程上创建COM对象。 The thread itself is running till your application exits (IsBackground = true) or you kill the thread explicitly with Thread.Abort(). 线程本身一直在运行,直到您的应用程序退出(IsBackground = true)或使用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. 但是请记住,所有对COM对象的调用都经过整理,因此在一个线程上一个接一个地序列化执行。 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. ASPCompat = true表示正在使用STA COM对象并因此在STA线程中运行页面的ASP.NET运行时。 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对象将在由您的页面的所有请求共享的自动生成的STA线程中运行(请参见MSDN, 网址为http : //msdn.microsoft.com/zh-cn/library/zwk9h2kb ( VS .80).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM