gpt4 book ai didi

.net - COM Interop - COM Callable Wrapper 中的多线程

转载 作者:行者123 更新时间:2023-12-01 04:11:51 25 4
gpt4 key购买 nike

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

例如,我有一个 .NET 程序集 dll,它向 COM 公开一个 .NET FTP 库。上传功能目前被编程为“尽力而为”功能。我没有使用事件或返回值来显示上传或下载是否成功完成,如有必要,我可能会考虑稍后实现。 .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 应用程序,我认为只要这些线程处于事件状态,底层进程就会继续执行。我建议您创建一个测试项目来确认您的应用程序的行为。

关于.net - COM Interop - COM Callable Wrapper 中的多线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5583081/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com