不盲目使用InvokeRequired只是坏的做法? [英] Isn't blindly using InvokeRequired just bad practice?

查看:1221
本文介绍了不盲目使用InvokeRequired只是坏的做法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手程序员,所以我可能是完全错误的在这里,但这个问题困扰我多那么它应该。

I am a novice programmer so I could be completely mistaken here, but this issue bugs me more then it should.

这实际上是一个后续< A HREF =http://stackoverflow.com/questions/747210/whats-wrong-with-calling-invoke-regardless-of-invokerequired>这个问题

接受的答案是,你必须调用InvokeRequired为了避免一些开销,因为你是在UI线程上已经运行的机会。

The accepted answer was, that you have to call InvokeRequired in order to avoid some overhead, because there is a chance you are already operating on the UI thread.

在理论上我同意,它可以节省一些时间。一些测试后,我发现,使用调用相比,调用一个操作通常需要约两倍的时间(测试,如设置的标签n次的文本,或放置一个非常,非常大的字符串中的一个RichTextBox)。

In theory, I agree that it could save some time. After some tests I found out that using Invoke takes about twice the time compared to calling an operation normally (tests like setting the text of a label n times, or placing a very, very big string in a RichTextBox).

但是再有就是实践

MSDN文档说:

此属性可用于确定是否必须调用Invoke方法,它可以是有用的,如果你不知道什么线程拥有控制权。

This property can be used to determine if you must call an invoke method, which can be useful if you do not know what thread owns a control.

在大多数情况下,您知道,当你尝试从另一个线程访问控制。其实只有情况我能想到的是,当控制从可以通过线程点¯x藏汉作为所有者线程调用方法来访问。这对我来说是一个不太可能的情况。

In most cases, you do know when you try to access a control from another thread. Actually the only situation I can think of is, when the control is accessed from a method that can be called by thread X aswell as the owner thread. And that to me is a very unlikely situation.

而且,即使你真的不知道哪个线程试图操纵控制,还有一个事实,即UI线程不必经常更新这一点。 25-30 FPS之间的任何东西应该没问题你的GUI。而大多数的用户界面的控件中所做的更改需要远小于毫秒来执行。

And even if you genuinely don't know which thread tries to manipulate the control, there is the fact that the UI thread doesn't have to be updated that frequently. Anything between 25-30 fps should be okay for your GUI. And most of the changes made in the UI-controls takes far less then milliseconds to perform.

所以,如果我理解corrrectly,你拥有的唯一方案,以检查是否有调用需要的是,当你不知道哪个线程访问控制,当GUI更新时间超过约40毫秒更长时间才能完成。

So if I understand corrrectly, the only scenario where you have to check if an invoke is required is when you don't know which thread is accessing the control and when the GUI update takes more than about 40 ms to finish.

再有就是答案来这个的问题,我问上的http://programmers.stackexchange.com 。其中规定,当你不需要它,你不应该忙于过早的优化。 。特别是如果它牺牲代码的可读性。

Then there is the answer to this question I asked on http://programmers.stackexchange.com. Which states that you shouldn't be busy with premature optimisation when you don't need it. Especially if it sacrifices code readability.

因此,这使我想到我的问题:你不应该只使用,当你知道不同的线程访问控制调用,和当你知道你的UI线程可以访问一段代码你会发现它应该运行得更快,那是否需要调用你应该检查?

So this brings me to my question: shouldn't you just use invoke when you know a different thread accesses a control, and only when you know your UI thread can access that piece of code and you find that it should run faster, that you should check if an Invoke is required?

PS:校对我的问题后,听起来真是我咆哮。但实际上我只是好奇,为什么InvokeRequired是看似有更多经验的超我的程序员过度使用。

PS: after proofreading my question it really sounds like I am ranting. But actually I am just curious why InvokeRequired is seemingly overused by many more-experienced-than-me programmers.

推荐答案

您正在服用事情在这里断章取义。 href=\"http://stackoverflow.com/questions/747210/whats-wrong-with-calling-invoke-regardless-of-invokerequired\">您链接的第一个问题线程安全的方法来访问UI控件。

You are taking things out of context here. The first question you linked linked another question which specifically was about writing a thread-safe method to access a UI control.

如果您不需要UI控件一个线程安全的访问,因为你知道你不会从另一个线程更新它的话肯定,你不应该使用这种技术。只要不使用 InvokeRequired 调用

If you don't need a thread-safe access to a UI control, because you know you won't update it from another thread, then certainly, you shouldn't employ this technique. Simply update your UI control without using InvokeRequired or Invoke.

在另一方面,如果呼叫会的总是的起源比UI线程以外的线程,只需使用调用 < 。code> InvokeRequired

On the other hand, if the call will always originate in a thread other than the UI thread, simply use Invoke without first checking for InvokeRequired.

这导致了三个简单的规则:

This leads to three simple rules:


  1. 如果您更新只能从UI线程控制,既不使用 InvokeRequired 也不调用

  2. 如果您只更新从非UI线程的线程上,只能使用调用
  3. $ b $控制b
  4. 如果您同时更新UI线程和其他线程的控制,使用调用 InvokeRequired 组合

  1. If you update the control only from the UI thread, use neither InvokeRequired nor Invoke
  2. If you update the control only from a thread other than the UI thread, use only Invoke.
  3. If you update the control from both the UI thread and other threads, use Invoke in combination with InvokeRequired.

这篇关于不盲目使用InvokeRequired只是坏的做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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