Invoke() 和 BeginInvoke() 有什么区别 [英] What's the difference between Invoke() and BeginInvoke()

查看:33
本文介绍了Invoke() 和 BeginInvoke() 有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想知道 BeginInvoke()Invoke() 之间的区别是什么?

Just wondering what the difference between BeginInvoke() and Invoke() are?

主要是每个人的用途.

创建线程对象并在其上调用 invoke 与仅在委托上调用 BeginInvoke() 之间有什么区别?还是它们是同一回事?

What is the difference between creating a threading object and calling invoke on that and just calling BeginInvoke() on a delegate? or are they the same thing?

推荐答案

你是说 Delegate.Invoke/BeginInvoke 还是 Control.Invoke/BeginInvoke?

Do you mean Delegate.Invoke/BeginInvoke or Control.Invoke/BeginInvoke?

  • Delegate.Invoke:在同一个线程上同步执行.
  • Delegate.BeginInvoke:在 threadpool 线程上异步执行.
  • Control.Invoke:在 UI 线程上执行,但调用线程在继续之前等待完成.
  • Control.BeginInvoke:在UI线程上执行,调用线程不等待完成.
  • Delegate.Invoke: Executes synchronously, on the same thread.
  • Delegate.BeginInvoke: Executes asynchronously, on a threadpool thread.
  • Control.Invoke: Executes on the UI thread, but calling thread waits for completion before continuing.
  • Control.BeginInvoke: Executes on the UI thread, and calling thread doesn't wait for completion.

Tim 的回答提到了您何时可能想要使用 BeginInvoke - 尽管我怀疑它主要面向 Delegate.BeginInvoke.

Tim's answer mentions when you might want to use BeginInvoke - although it was mostly geared towards Delegate.BeginInvoke, I suspect.

对于 Windows 窗体应用程序,我建议您通常使用 BeginInvoke.这样你就不必担心死锁,例如 - 但你需要明白在你下次查看时 UI 可能还没有更新!特别是,您不应修改 UI 线程可能将用于显示目的的数据.例如,如果您有一个带有 FirstNameLastName 属性的 Person,并且您做到了:

For Windows Forms apps, I would suggest that you should usually use BeginInvoke. That way you don't need to worry about deadlock, for example - but you need to understand that the UI may not have been updated by the time you next look at it! In particular, you shouldn't modify data which the UI thread might be about to use for display purposes. For example, if you have a Person with FirstName and LastName properties, and you did:

person.FirstName = "Kevin"; // person is a shared reference
person.LastName = "Spacey";
control.BeginInvoke(UpdateName);
person.FirstName = "Keyser";
person.LastName = "Soze";

那么用户界面很可能最终会显示Keyser Spacey".(它有可能显示Kevin Soze",但只是由于内存模型的奇怪.)

Then the UI may well end up displaying "Keyser Spacey". (There's an outside chance it could display "Kevin Soze" but only through the weirdness of the memory model.)

除非您遇到此类问题,否则 Control.BeginInvoke 更容易解决,并且可以避免您的后台线程无缘无故地等待.请注意,Windows 窗体团队已保证您可以以即发即忘"的方式使用 Control.BeginInvoke - 即无需调用 EndInvoke.这通常不适用于异步调用:通常每个 BeginXXX 都应该有一个对应的 EndXXX 调用,通常在回调中.

Unless you have this sort of issue, however, Control.BeginInvoke is easier to get right, and will avoid your background thread from having to wait for no good reason. Note that the Windows Forms team has guaranteed that you can use Control.BeginInvoke in a "fire and forget" manner - i.e. without ever calling EndInvoke. This is not true of async calls in general: normally every BeginXXX should have a corresponding EndXXX call, usually in the callback.

这篇关于Invoke() 和 BeginInvoke() 有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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