none
Write VisualBasic .NET DLL Assemblies which utilise the ApartmentState.MTA RRS feed

  • Question

  • 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.'

    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.

    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?

    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?

    Friday, March 9, 2018 2:20 PM

All replies

  • Hi AndyDubsSE,

    thanks for posting here.

    >>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?

    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?

    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.

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

    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.

    Hope this could be help of you.

    Best Regards,

    Baron Bi


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Baron Bi Saturday, March 17, 2018 2:17 AM
    Saturday, March 10, 2018 7:20 AM