为什么有人想要使用 Invoke() (而不是 BeginInvoke())?

发布于 2024-09-27 10:29:59 字数 138 浏览 0 评论 0原文

有人告诉我 Invoke() 与普通方法调用类似...那么为什么人们会选择使用 Invoke 而不是普通方法调用呢?

我尝试在网上搜索有关该问题的信息,我得到的是使用 BeginInvoke() 的优点,但是使用 Invoke() 的优点是什么?

I was told that Invoke() is similar to normal method calling... so why would people choose to use Invoke and not normal method calling?

I tried to search online regarding the issue, what i get is the advantages of using BeginInvoke(), but what are the advantages of using Invoke()?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

各自安好 2024-10-04 10:29:59

当您想要异步调用委托(在从线程池中提取的线程上)时,请使用 BeginInvoke ;如果您想要同步调用委托,请使用 Invoke

Use BeginInvoke when you want to call the delegate asynchronously (on a thread drawn from the thread pool) and Invoke if you want to call it synchronously.

北恋 2024-10-04 10:29:59

首先值得注意的是,我们必须有像 .Invoke() 这样的东西作为语言之间的交汇点。正如 C# 中的 int 和 VB.NET 中的 Integer 是它们两者以及任何其他 CLR 语言的 System.Int32 一样;所以我们也有 Invoke() ,任何 CLR 语言都可以访问它,然后它们可以以适合其语法的方式提供额外的语法糖(大多数人会在 C# 或 C# 中重点考虑 VB 风格约定) VB 中的 C# 风格约定是语法醋;糖总是必须尽可能与语言的其余部分相匹配)。

这个加了糖,为什么不一直用呢?有些人有时会想清楚他们正在与代表打交道。有些人就是不会使用它;有些人则不会使用它。我大多不这样做。与所有语法糖一样,其优点和缺点在于清晰度而不是正确性(事实上,查看反射器中对 x.Invoke() 的调用,您会发现它为 x () 因为反射器不知道你使用的是哪一个)。

It's worth noting first that we have to have something like .Invoke() as the meeting-point between languages. Just as what is int to C# and Integer to VB.NET is System.Int32 to both of them, and to any other CLR language; so too we have Invoke() which any CLR language can offer access to, and which they can then provide additional syntactic sugar in a means suitable for their syntax (most would consider heavily VB style conventions in C# or heavily C# style conventions in VB to be syntactic vinegar; sugar always has to match the rest of the language as best it can).

This sugar added, why not use it all the time? Some people will sometimes want to be clear that they are dealing with a delegate. Some just won't use it; I mostly don't. Like all syntactic sugar, the advantages and disadvantages are matters of clarity rather than correctness (indeed, look at a call to x.Invoke() in reflector and you'll see that it as x() because reflector doesn't know which you used).

长亭外,古道边 2024-10-04 10:29:59

原因之一是,如果您通过反射获得了方法签名,并且您想要动态实例化一个函数,那么 Invoke() 就是您执行此操作的方式。

One reason is if you've gotten your method signature through reflection and you dynamically want to instantiate a function, Invoke() is the way you'd do that.

執念 2024-10-04 10:29:59

例如,为了改变正在执行某些操作的线程。

请注意,任何 UI 控件都应该并且始终只能从创建它的线程(实际上是消息泵)进行操作。因此,如果另一个线程正在操纵该控件(从其他线程的角度来看是同步的),那么 BeginInvoke 将是额外的开销,但 Invoke 很好(特别是因为至少在 WPF 中,这里有一个用于多个调用序列的快捷方式,可以使其更快)内部执行)。

In order to change, for example, the thread executing something.

Note that any UI control shall and can alweays only be manipulated from the thread that created it (message pump, actually). So, if another thread is manipulating the control (synchronously from that other threads point of view) then BeginInvoke would be additional overhead, but Invoke is fine (ESPECIALLY because at least in WPF there is a shortcut herere for multiple invoke sequences that make it faster to execute internally).

猫烠⑼条掵仅有一顆心 2024-10-04 10:29:59

我想您已经了解委托是什么、用途是什么以及为什么有用。

如果我有一个名为“MyDelegate”的删除门,我可以执行“MyDelegate(foo);”或“MyDelegate.Invoke(foo);”,但我总是使用第二个,这样当我检查代码时我可以很容易地看到,这实际上是一个委托而不是一个方法。没有内部差异。

I guess you already understand what a delegate is, what is for, and why is useful.

If I have a deletegate named "MyDelegate", I can do "MyDelegate(foo);" or "MyDelegate.Invoke(foo);" , but I always use the second one so I could easily see when I review the code, that is actually a delegate and not a method. There is no internal difference.

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