远程对象上的 DynamicInvoke 是否异步工作?
我继承了一些主要的意大利面条代码(C#/VB 组合),我想在这里理解它们。
这似乎是一种非常奇怪的情况,其中有两次连续调用向远程对象触发事件,这是通过调用委托的 DynamicInvoke 方法来完成的,其形式为:
delegate1.DynamicInvoke(args1);
// some code here
delegate2.DynamicInvoke(args2);
delegate1 和 delegate2 都引用同一个远程中的方法“订阅者”对象。
根据我在文档中读到的所有内容, DynamicInvoke 看起来应该是同步的。 但是,当我在远程进程中放置断点时,我可以在眼前看到 delegate1 和 delegate2 引用的方法正在不同的线程中同时运行。
这是微软的另一个“未记录的功能”吗? 我应该预料到会这样吗? 关于为什么会这样的任何解释吗? 如果它是异步运行的,DynamicInvoke 如何有返回值?
谢谢! 沙乌尔
I have inherited some majorly spaghetti code (combination C#/VB) that I'm trying to understand here.
This appears to be a really weird situation where there are two successive calls to fire events to a remote object, which is being done by calling the DynamicInvoke method of a delegate, of the form:
delegate1.DynamicInvoke(args1);
// some code here
delegate2.DynamicInvoke(args2);
delegate1 and delegate2 both refer to methods in the same remote "subscriber" object.
According to everything I can read in the documentation, DynamicInvoke looks like it should be synchronous. But I can see, before my eyes, when I put breakpoints into the remote process, that the methods referred to by delegate1 and delegate2 are running simultaneously, in different threads.
Is this another Microsoft "undocumented feature"? Should I have expected this? Any explanations as to why this should be? And if it's meant to run asynchronously, how can DynamicInvoke have a return value?
Thanks!
Shaul
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
DynamicInvoke 绝对是同步操作。 但问题是它指向的委托不是同步的。 例如,以下代码将以异步方式调用 Console.WriteLine()。
del1 将同步执行,但它将创建一个异步工作项。
我会查找委托正在执行的代码,看看他们是否在幕后执行一些异步魔法。
DynamicInvoke is definately a synchronous operation. The problem though is that the delegate it points to make not be synchronous. For instance the following code will call Console.WriteLine() in an asynchronous fashion.
del1 will be executed synchronously but it will create an asynchronous work item.
I would look for the code the delegate is executing and see if they are doing some asyc magic under the hood.
我的主要建议是仔细检查两个进程中的调用堆栈。
您的“意大利面条”代码是否可能实际上有一个回调。
您的第一个委托调用您的远程进程,它回调,并且您的第二个委托正在从嵌套回调中调用?
My main advice would be to inspect the call stacks carefully in your two processes.
Is it possible that your "spaghetti" code actually has a callback in it.
Your first delegate calls your remote process, it calls back, and your second delegate is being invoked from the nested callback?