COM 互操作 .NET STA

发布于 2024-10-07 16:57:35 字数 309 浏览 1 评论 0原文

如果我在 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

┼── 2024-10-14 16:57:35

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

如果您在 .NET MTA 线程上创建 STA COM 对象,运行时将创建一个 STA 线程并编组对该线程的所有调用。

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

解决方案可能是在新线程上创建对象,您可以控制其生命周期。

我做了类似的事情:

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() 显式终止线程。

但请记住,对 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:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文