在多个线程中使用时,JACOB库失败 [英] JACOB library fails when used in multiple threads

查看:281
本文介绍了在多个线程中使用时,JACOB库失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从两个因此开始的相同线程中使用JACOB遇到了一个奇怪的问题.我有一个实用程序类,它使用静态ActiveXObject字段将各种请求分派到WMI.第一个线程工作正常.当第二个线程启动时,出现以下异常:

I have a strange problem using JACOB from two consequently started identical threads. I have a utility class that uses a static ActiveXObject field for dispatching various requests to the WMI. The first thread works fine. When the second thread is started I get the following exception:

com.jacob.com.ComFailException: Can't map name to dispid: ExecQuery
at com.jacob.com.Dispatch.invokev(Native Method)
at com.jacob.com.Dispatch.invokev(Dispatch.java:858)
at com.jacob.com.Dispatch.callN(Dispatch.java:455)
at com.jacob.com.Dispatch.call(Dispatch.java:544)
at com.jacob.activeX.ActiveXComponent.invoke(ActiveXComponent.java:510)
at JacobWmiAdapter.getResultsList(JacobWmiAdapter.java:111)
at JacobWmiAdapter.getResultsList(JacobWmiAdapter.java:104)
at WindowsInfoCollector.getConnectionInfo(WindowsInfoCollector.java:516)
at WindowsInfoCollector.collect(WindowsInfoCollector.java:118)
at DiagnosisExecutor.execute(DiagnosisExecutor.java:128)
at DiagnosisExecutor.run(DiagnosisExecutor.java:160)
at java.lang.Thread.run(Thread.java:662)

通过在GUI中单击鼠标即可启动线程,但是该错误可以手动重现:

The thread is started by a mouse click from a GUI, but the error is reproducible manually:

        DiagnosisExecutor dex = new DiagnosisExecutor();
        Thread thread1 = new Thread(dex);
        Thread thread2 = new Thread(dex);

        thread1.start();
        thread1.join();
        Thread.sleep(1000);
        thread2.start();

在我看来,使用它们的线程终止时,某些分配的资源未正确释放.有提示吗?

It seems to me that some allocated resources are not being released correctly when the thread that uses them terminates. Any hints?

更新:JACOB版本1.14.3

Update: JACOB Version 1.14.3

推荐答案

我尚未使用最新版本的JACOB及其新的线程模型,但较早的版本肯定是 not 线程安全.从1.7版开始,他们已经改进了线程模型以更好地反映基础COM组件,但是您必须确定该组件是MTA还是STA,然后适当地初始化JACOB类.请参阅 JACOB文档,以了解如何根据其需求适当地调整您的应用程序.

I haven't used the latest version of JACOB with it's new threading model, but older versions were definitely not thread safe. As of version 1.7 > they have improved the threading model to better reflect the underlying COM components, but you have to determine whether said component is MTA or STA, and then initialize JACOB classes appropriately. Refer to the JACOB documentation for how to properly adapt your application according to its requirements.

这篇关于在多个线程中使用时,JACOB库失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆