.netcom-interopccw

COM Interop - Multi-Threading in a COM Callable Wrapper


Is it possible to use multi-threading in a .NET COM callable wrapper DLL assembly?

For example, I have a .NET assembly dll that exposes a .NET FTP library to COM. Upload functions are currently programmed as "best effort" functions. I'm not using events or return values to show whether an upload or download completed successfully, which is something I may consider implementing later if necessary. Could the .NET assembly (in my really limited understanding) simply farm out these upload and download processes to another thread? Assuming this is possible, what would be the result of this if the hosting application was closed before the uploads or downloads were all completed?


Solution

  • Yes, MSDN article Managed and Unmanaged Threading explains the details. In particular:

    For interoperability, the common language runtime creates and initializes an apartment when calling a COM object. A managed thread can create and enter a single-threaded apartment (STA) that contains only one thread, or a multi-threaded apartment (MTA) that contains one or more threads. When a COM apartment and a thread-generated apartment are compatible, COM allows the calling thread to make calls directly to the COM object. If the apartments are incompatible, COM creates a compatible apartment and marshals all calls through a proxy in the new apartment.


    what would be the result of this if the hosting application was closed before the uploads or downloads were all completed?

    Good question. How are you terminating the application? If it is simply a GUI application that you are closing, I think the underlying process would continue to execute as long as those threads are active. I recommend you create a test project to confirm your application's behavior.