有什么不对调用调用,不管InvokeRequired的? [英] What's wrong with calling Invoke, regardless of InvokeRequired?

查看:129
本文介绍了有什么不对调用调用,不管InvokeRequired的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了跨线程访问GUI控制,如这里所讨论的常用的设置:
http://stackoverflow.com/questions/571706/shortest-way-to-write-a-thread-safe-访问方式到一个窗口表单控制

I've seen the common setup for cross threading access to a GUI control, such as discussed here: http://stackoverflow.com/questions/571706/shortest-way-to-write-a-thread-safe-access-method-to-a-windows-forms-control

所有的网络点击率,我发现描述了类似的事情。

All the web hits I found describe a similar thing.

然而,为什么我们需要检查InvokeRequired?我们不能只叫直接调用?

However, why do we need to check InvokeRequired? Can't we just call Invoke directly?

我认为答案是否定的,所以我的真正的问题是为什么?

I assume the answer is no, so my real question is 'why'?

推荐答案

从我们不能触碰UI非UI线程 - 非常糟糕的事情都可能发生,因为控件具有线程关联。因此,从非UI线程,我们必须(在minumum)调用调用的BeginInvoke

From non-UI threads we can't touch the UI - very bad things can happen, since controls have thread affinity. So from a non-UI thread we must (at a minumum) call Invoke or BeginInvoke.

有关UI线程,但是 - 我们的的要拨打许多调用的时间;问题是,如果你的的已经在UI线程上,它仍然有发送消息到窗体的泵和处理它的不必要的开销。

For UI-threads, however - we don't want to call Invoke lots of time; the issue is that if you are already on the UI thread, it still has the unnecessary overhead of sending a message to the form's pump and processing it.

在现实中,大多数线程代码,你的知道的你期望被称为上的 -UI螺纹上的特定方法,所以在这种情况下,没有额外的开销:只需拨打调用

In reality, in most threading code you know you expect a specific method to be called on a non-UI thread, so in those cases, there is no additional overhead: just call Invoke.

这篇关于有什么不对调用调用,不管InvokeRequired的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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