gpt4 book ai didi

c# - 从多线程 c# Windows 服务应用程序调用 VB6 DLL?

转载 作者:太空狗 更新时间:2023-10-29 19:44:43 27 4
gpt4 key购买 nike

我正在运行一个需要调用 VB6 dll 的多线程 Windows 服务。没有关于这个 VB6 dll 的文档,这个遗留系统支持一个非常关键的业务流程。

第一次(第一个线程),这个 dll 表现良好。由于其他线程需要访问,它开始提供错误的结果。

我读到一个人说:

"Just be careful of one thing if you are using VB6. Your threading model is going to have to change to support apartments if you are running a multithreaded service. VB only supports multiple single-threaded apartments, but .NET runs fully free threaded normally. The thread that calls into the VB6 DLL needs to be compatible with the DLL."

团队中的另一个人给了我将这个 ddl 放在一个单独的应用程序域中的想法。但我不确定。

我们如何使用从多线程 C# Windows 服务应用程序调用的 VB6 dll?

最佳答案

当线程进入时,您是否保存对象并稍后在新线程上重用它们?如果可以,为每个线程创建新的对象。我们使用的数据层 dll 遇到了这样的情况。如果您在一个线程上创建连接,则不能从另一个线程使用它。如果您在每个线程上创建一个新连接,它就可以正常工作。

如果创建对象很慢,请查看 ThreadPool 类和 ThreadStatic 属性。线程池一遍又一遍地回收同一组线程来完成工作,而 ThreadStatic 允许您创建一个只为一个线程存在的对象。例如

[ThreadStatic]
public static LegacyComObject myObject;

随着请求的到来,将其转换为作业并将其放入您的线程池中。作业启动时,检查静态对象是否初始化;

void DoWork()
{
if (myObject == null)
{
// slow intialisation process
myObject = New ...
}

// now do the work against myObject
myObject.DoGreatStuff();
}

关于c# - 从多线程 c# Windows 服务应用程序调用 VB6 DLL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1080759/

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