C# - ActiveX控件没有形式 [英] C# - ActiveX control without a form

查看:250
本文介绍了C# - ActiveX控件没有形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们需要使用第三方ActiveX控件。

We are required to use a 3rd party ActiveX control.

唯一的问题是,在我们的软件层是业务层和有窗口或形式进行访问。它还可以运行在单独的线程(并且应该从任何线程)是不STA。

The only issue is, the layer in our software is a business layer and has no access to a window or form. It also runs on separate threads (and should work from any thread) that are not STA.

而不是从业务逻辑打破了我们分离的用户界面,我们用这样的解决方法,使其工作:

Rather than breaking our separation of UI from business logic, we used this workaround to make it work:

Thread thread = new Thread((ThreadStart)
delegate
{
_myActiveX = new MyActiveXType();
_myActiveX.CreateControl();

//more initialize work

Application.Run();
});
thread.SetApartmentState(ApartmentState.STA);
thread.IsBackground = true;
thread.Start();

那么任何时候,我们需要引用的控制,我们称之为_myActiveX.BeginInvoke()或调用()。

Then anytime we need to reference the control, we call _myActiveX.BeginInvoke() or Invoke().

在处理这一类的(退出我们的应用程序),我们处理的控制并中止线程。

On disposing of this class (exiting our app), we dispose the control and abort the thread.

我的问题是,是否有任何与此问题?有没有更好的方式来处理呢?

My question is, are there any problems with this? Is there a better way to handle this?

有没有更好的内置的方式从内部未知的多线程环境中使用ActiveX控件的工作?我们正在尝试写我们的类,它包装的控制,但将努力从任何线程的方式。

Is there a better built in way to work with an ActiveX control from within an unknown multi-threaded environment? We are trying to write our class in a way that wraps the control but will work from any thread.

更新:作为一个答复建议,我们真的宁愿使用标准的COM对象,而不是用控制的。我们与这个问题是我们会得到错误(从HRESULT异常:0x8000FFFF(E_UNEXPECTED)。在第一方法或属性,我们的COM对象调用这是一个pretty的一般性错误,我们没有得到的时候使用ActiveX,任何想法?

UPDATE: As an answer suggested, we really would rather use the standard COM object and not use a control at all. Our issue with that was we would get the error "(Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED)" upon the first method or property we call on the COM object. This is a pretty generic error that we don't get when using the ActiveX, any ideas?

更新:我们的OCX是CX25.ocx,使用tlbimp.exe是我们得到CX25Lib.dll。使用aximp.exe,我们得到AxCX25Lib.dll和CX25Lib.dll。 CX25Lib.dll不会在任一情况下工作。 AxCX25Lib.dll工作。

UPDATE: Our ocx is "CX25.ocx", using tlbimp.exe we get CX25Lib.dll. Using aximp.exe, we get AxCX25Lib.dll and CX25Lib.dll. CX25Lib.dll does not work in either case. AxCX25Lib.dll works.

推荐答案

我假设这是正确的方式去了解这一点。

I'm assuming this is the proper way to go about this.

我们一直在使用我的code以上的测试环境,在过去的几个星期,没有问题。

We've been using my code above in test environments for the past few weeks with no issues.

如果任何人有使用ActiveX没有一个形式,我认为这是做这件事。

If anyone has to use an ActiveX without a form, I assume this is one way to do it.

只要确保后直接您的ActiveX对象的构造函数调用_yourActiveXControl.CreateControl()。这简化了我们原本的许多问题。

Just make sure to call _yourActiveXControl.CreateControl() directly after your ActiveX object's constructor. This simplified many issues we had originally.

这篇关于C# - ActiveX控件没有形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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