在C#客户端中处理COM回调 [英] Handling COM Callbacks in C# Client

查看:97
本文介绍了在C#客户端中处理COM回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是应用程序的设计方式-
我有一个COM服务器,在某些情况下会引发事件。
我有一个C#客户端UI,该UI应该处理此COM服务器引发的这些事件。

Here is how the application is designed - I have a COM server raising events on certain conditions. I have a C# client UI which is supposed to handle these events raised by this COM server.

我的C#客户端中有一个委托,我将其传递给COM服务器,并且该委托会回调事件。我有一个自定义包装器,将委托代理封送为函数指针。像这样...

There is a delegate in my C# client which I pass to the COM server and it calls back on an event. I have a custom wrapper which marshals the delegate as function pointer. Like so...

     Fxn1inCOMWrapper([In, MarshalAs(UnmanagedType.FunctionPtr)] Client.SetStatusDelegate StatusCallback);

回调机制有效。
这里的问题是,一旦获得回调,就应该在COM Server中调用另一个方法来调用进一步的处理。

The callback mechanism works. The problem here is that once I get the callback, I am supposed to call another method in the COM Server to invoke further processing.

我无法从C#回调方法对COM组件进行第二次函数调用,它破坏了COM中的堆栈,并向我返回了异常。
我猜是这样,因为回调与在其上创建COM对象并进行第一个调用的线程不同。

I am not able to make the second function call to COM component from the C# callback method, and it corrupts the stack in COM and it returns me an exception. I am guessing it is the case because the callback is not the same thread on which the COM object was created and the first call was made. Is it possible to work around this problem?

一个选择是在C#客户端的事件回调中设置一个标志。然后在主线程的计时器上检查该标志,如果是,请进行第二次调用。但是肯定有一种更优雅的方式可以做到这一点...有人可以帮忙吗?

One option was to set a flag in event callback in C# client. And on a timer from the main thread, check for the flag and if yes, make the second call. But there surely has to be a more elegant way of doing this...Can anyone help please?

推荐答案

尝试更改线程 Main()函数的属性。例如,如果您有:

Try changing the threading attribute on your Main() function. For example, if you have:

[STAThread]
static void Main()

将其更改为:

[MTAThread]
static void Main()

这篇关于在C#客户端中处理COM回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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