为什么要为 InvokeRequired 烦恼

发布于 2024-12-02 05:50:08 字数 441 浏览 0 评论 0原文

我了解需要使用 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 技术交流群。

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

发布评论

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

评论(2

机场等船 2024-12-09 05:50:08

让 UI 线程对其本身最终执行的方法调用 Invoke 会产生任何后果吗?

据我所知,唯一的区别是,如果在创建控件句柄之前调用,则使用 Invoke 将会失败。

这个 文章更详细地讨论了这些问题。

Are there any consequences to having the UI thread call Invoke on a method which it will itself end up executing?

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.

情绪失控 2024-12-09 05:50:08

让 UI 线程对其本身最终执行的方法调用 Invoke 会产生任何后果吗?

不,没有任何后果,除了可能的性能,就像不需要调用一样,直接方法调用将比通过 Invoke 基础设施更快。

Are there any consequences to having the UI thread call Invoke on a method which it will itself end up executing?

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.

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