.net 中 Invoke() 有什么用?
我找到了这段代码:
this.Invoke(new EventHandler(EventGetSum));
这和写:
EventGetSum();
这有什么用?
I found this code:
this.Invoke(new EventHandler(EventGetSum));
Is this not the same as writing:
EventGetSum();
What's the use of this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您编写
EventGetSum()
,它会立即调用 EventGetSum 方法。如果您编写
new EventHandler(EventGetSum)
来创建一个委托,该委托将在调用时调用 EventGetSum。调用
Control.Invoke
从负责控件的 UI 线程调用给定的委托。 这是必要的,因为您不能从任意线程访问 UI 元素。
If you write
EventGetSum()
that immediately calls the EventGetSum method.If you write
new EventHandler(EventGetSum)
that creates a delegate which will (in turn) call EventGetSum when it's invoked.The call to
Control.Invoke
invokes the given delegate from the UI thread responsible for the control. This is necessary because you mustn't access UI elements from arbitrary threads.它在
this
窗口所属的线程中执行EventGetSum
方法。It executes the
EventGetSum
method in the thread to that the windowthis
belongs to.这通常在处理跨线程 UI 调用时使用。
查看 ISynchronizeInvoke 的 MSDN 文档。
This is normally used when dealing with cross thread UI calls.
Look at the MSDN documentation for ISynchronizeInvoke.