为什么要为 InvokeRequired 烦恼
我了解需要使用 Invoke/BeginInvoke 从工作线程调用对属于 UI 线程的组件进行更改的函数或过程...
我的问题是 - 是否有充分的理由去检查 InvokeRequired?假设在不同情况下可以从工作线程或 UI 线程调用事件处理程序。让 UI 线程对其本身最终执行的方法调用 Invoke 会产生任何后果吗?
说...
Private Sub SomeProcedure() Handles event1, event2, event3
Me.Invoke(New delegateSomeProc(Address of SomeProc))
EndSub
现在,假设 event1 从 UI 线程触发,但 Events 2 和/或 3 从其他线程触发... event1 调用 invoke 是否有任何危险,即使它不是必须的?是不是效率有点低?
I understand the need to use Invoke/BeginInvoke to make calls from worker threads to functions or procedures which make changes to components which belong to the UI thread...
My question is - is there a good reason to bother checking InvokeRequired? Say an event handler may be called either from a worker thread or from the UI thread in different circumstances. Are there any consequences to having the UI thread call Invoke on a method which it will itself end up executing?
Say...
Private Sub SomeProcedure() Handles event1, event2, event3
Me.Invoke(New delegateSomeProc(Address of SomeProc))
EndSub
Now, say event1 fires from the UI thread but Events 2 and/or3 fire from some other thread... is there any peril to event1 calling invoke anyway, even though it doesn't have to? Is it just a bit inefficient?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知,唯一的区别是,如果在创建控件句柄之前调用,则使用 Invoke 将会失败。
这个 文章更详细地讨论了这些问题。
The only difference that I know of is that using
Invoke
will fail if called before the control's handle has been created.This article discusses the issues in more detail.
不,没有任何后果,除了可能的性能,就像不需要调用一样,直接方法调用将比通过 Invoke 基础设施更快。
No, there are no consequences, other than probably performance as if no invoke is required, a direct method call will be faster than passing through the Invoke infrastructure.