我在使用.NET编写投掷警告,看起来像我的COM组件的麻烦:从WinForms GUI线程调用COM线程方法的问题?
上下文0x15eec0断开。没有 代理将用于服务COM组件上的 请求。这可能会导致数据损坏或数据丢失 。为了 避免此问题,请确保 所有上下文/公寓活路 直到应用程序完全 与代表 住在他们里面COM组件RuntimeCallableWrappers 完成。
看起来这是由我的GUI线程调用COM线程中的函数而没有必要的同步引起的。作为参考,我使用在http://msdn.microsoft.com/en-us/library/ms229609%28VS.80%29.aspx中设置的准则来在COM组件中创建我的GUI线程。
我的代码看起来是这样的:
class COMClass {
// this is called before SomeMethod
public void Init() {
ComObject comObject = new ComObject(); // this is imported from a TLB
// I create my GUI thread and start it as in the MSDN sample
Thread newThread = new Thread(new ThreadStart(delegate() {
Application.Run(new GUIForm(comObject));
}));
}
public void SomeMethod(){
comObject.DoSomething(); // this is where the error occurs
}
}
class GUIForm : Form {
ComObject com;
public GUIForm(ComObject com) {comObject = com;}
public void SomeButtonHandler(object sender, EventArgs e) {
comObject.SomeMethod(); // call on the GUI thread but the com object is bound to the COM thread...
}
}
是否有处理这个既定的方法?调用GUI是没有问题的(Invoke/BeginInvoke),但调用其他方式似乎更困难...
编辑:它也不是一个选项修改COM对象的任何方式。
这是不适用于此应用程序的COM系统的设计。没有暴露的循环,我可以插入代码来使用队列中的事件;如果我将一个循环添加到在我的COM对象上调用的任何函数,它将永远不会返回到调用应用程序。 – 2010-02-18 23:18:34
坦率地说,我希望有某种ThreadBoundInvoker,我可以发布事件来执行绑定的线程。 – 2010-02-18 23:20:03
是否仅由GUIForm使用COM对象?如果是这样,你可以在窗体中实例化它,并且所有的东西都在GUI线程中。 – PatrickSteele 2010-02-19 01:27:44