简体   繁体   English

COM Interop-COM可调用包装程序中的多线程

[英]COM Interop - Multi-Threading in a COM Callable Wrapper

Is it possible to use multi-threading in a .NET COM callable wrapper DLL assembly? 是否可以在.NET COM可调用包装DLL程序集中使用多线程?

For example, I have a .NET assembly dll that exposes a .NET FTP library to COM. 例如,我有一个.NET程序集dll,它将.NET FTP库公开给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? .NET程序集(以我的有限理解)是否可以将这些上载和下载过程简单地分配到另一个线程? 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? 假设这是可能的,那么如果在上载或下载全部完成之前关闭了托管应用程序,结果将是什么?

Yes, MSDN article Managed and Unmanaged Threading explains the details. 是的,MSDN文章“ 托管和非托管线程”介绍了详细信息。 In particular: 特别是:

For interoperability, the common language runtime creates and initializes an apartment when calling a COM object. 为了实现互操作性,公共语言运行库在调用COM对象时创建并初始化一个单元。 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. 托管线程可以创建并输入仅包含一个线程的单线程单元(STA),或包含一个或多个线程的多线程单元(MTA)。 When a COM apartment and a thread-generated apartment are compatible, COM allows the calling thread to make calls directly to the COM object. 当COM单元和线程生成的单元兼容时,COM允许调用线程直接对COM对象进行调用。 If the apartments are incompatible, COM creates a compatible apartment and marshals all calls through a proxy in the new apartment. 如果这些公寓不兼容,COM将创建一个兼容公寓并通过新公寓中的代理封送所有呼叫。


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. 如果您只是要关闭的一个GUI应用程序,我认为只要这些线程处于活动状态,基础进程就会继续执行。 I recommend you create a test project to confirm your application's behavior. 我建议您创建一个测试项目以确认您的应用程序的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM