.net – COM Interop – COM可调用包装器中的多线程

是否可以在.NET COM可调用包装器DLL程序集中使用多线程?

例如,我有一个.NET程序集dll,它将.NET FTP库暴露给COM.上传功能目前被编程为“尽力而为”功能.我没有使用事件或返回值来显示上传或下载是否成功完成,这是我可能考虑在以后必要时实施的内容. .NET程序集(在我真正有限的理解中)是否可以将这些上载和下载过程简化为另一个线程?假设这是可能的,如果托管应用程序在上传或下载全部完成之前关闭,那将会是什么结果呢?

最佳答案 是的,MSDN文章
Managed and Unmanaged Threading解释了详细信息.特别是:

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?

好问题.你是如何终止申请的?如果它只是一个关闭的GUI应用程序,我认为只要这些线程处于活动状态,底层进程就会继续执行.我建议您创建一个测试项目以确认您的应用程序的行为.

点赞