远程对象上的 DynamicInvoke 是否异步工作?

发布于 2024-07-10 12:35:22 字数 652 浏览 12 评论 0原文

我继承了一些主要的意大利面条代码(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野侃 2024-07-17 12:35:22

DynamicInvoke 绝对是同步操作。 但问题是它指向的委托不是同步的。 例如,以下代码将以异步方式调用 Console.WriteLine()。

Action write = () => Console.WriteLine("foo");
Action del1 = () => { ThreadPool.QueueUserWorkItem(write); }
...
del1.DynamicInvoke();

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.

Action write = () => Console.WriteLine("foo");
Action del1 = () => { ThreadPool.QueueUserWorkItem(write); }
...
del1.DynamicInvoke();

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.

红衣飘飘貌似仙 2024-07-17 12:35:22

我的主要建议是仔细检查两个进程中的调用堆栈。

您的“意大利面条”代码是否可能实际上有一个回调。
您的第一个委托调用您的远程进程,它回调,并且您的第二个委托正在从嵌套回调中调用?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文