编写使用ApartmentState.MTA的VisualBasic .NET DLL程序集 [英] Write VisualBasic .NET DLL Assemblies which utilise the ApartmentState.MTA

查看:101
本文介绍了编写使用ApartmentState.MTA的VisualBasic .NET DLL程序集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎不可能编写使用ApartmentState.MTA的VisualBasic .NET DLL程序集,这会导致终结器终结程序线程停滞,从而导致"内存泄漏"。

It does not appear to be possible to write VisualBasic .NET DLL Assemblies which utilise the ApartmentState.MTA, and this is leading to instances of stalled finalizer thread, and thus 'memory leaks.'

没有明显的机制来"促进"用VB编写的程序集,以便它们使用ApartmentState.MTA COM线程模型。如果这些是VB控制台应用程序,MTAThreadAttribute可能已被用于装饰Main()方法调用。
但是,这些有问题的VB DLL程序集被加载到现有的多线程应用程序中。

There is no apparent mechanism to 'promote' assemblies written in VB so that they utilise the ApartmentState.MTA COM threading model. If these had been VB console applications, the MTAThreadAttribute could have been used to decorate the Main() method call. However, these problematic VB DLL Assemblies are loaded into an existing multithreaded application.

是否有一种机制来编译VB程序集,因此它们在加载时默认为ApartmentState.MTA COM线程模型?如果没有,这是VB编译器中的错误吗?

Is there a mechanism to compile the VB assemblies so they default to ApartmentState.MTA COM threading model on loading? If there is not, is this a fault in the VB compiler?

是否有机制/解决方法将少量C#代码添加到现有(大)VB源代码中以生成IL字节码组件因此在加载时默认为ApartmentState.MTA COM线程模型?

Is there a mechanism / workaround to add a small amount of C# code to the existing (large) VB source code to produce IL bytecode assemblies so they default to ApartmentState.MTA COM threading model on loading?

推荐答案

嗨AndyDubsSE,

Hi AndyDubsSE,

感谢您在此处发布。

>>是否有机制来编译VB程序集,因此它们默认到加载的ApartmentState.MTA COM线程模型?如果没有,这是VB编译器中的错误吗?

是否有机制/解决方法将少量C#代码添加到现有代码中(用于生成IL字节码组件的VB源代码,因此在加载时默认为ApartmentState.MTA COM线程模型?

从.NET Framework 2.0版开始,默认线程COM互操作的模型取决于您开发应用程序的语言。例如,默认情况下,visual basic是单线程单元。

Starting with the .NET Framework version 2.0, the default threading model for COM interop depends on the language in which you are developing your application. For example, the visual basic is Single-threaded apartment as default.

当您开发一个调用C#库的Visual Basic应用程序时,该库反过来依赖于COM互操作, 您应该使用MTAThreadAttribute属性将应用程序的线程模型更改为多线程。因为多线程单元模型
是C#的默认值。

When you're developing a Visual Basic app that calls to a C# library that in turn relies on COM interop,  you should change your app's threading model to multithreaded by using the MTAThreadAttribute attribute. Because the multithreaded apartment model is the default for C#.

要更改这些默认值,可以使用MTAThreadAttribute属性为应用程序设置线程模型,或者调用

Thread.SetApartmentState

Thread.TrySetApartmentState
方法在启动线程之前设置特定线程的线程模型。

To change these defaults, you use the MTAThreadAttribute attribute to set the threading model for the application, or call the Thread.SetApartmentState or Thread.TrySetApartmentState method before starting the thread to set the threading model for a particular thread.

希望这可以帮到你。

最好的问候,

Baron Bi


这篇关于编写使用ApartmentState.MTA的VisualBasic .NET DLL程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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