为什么要在.net中的控件上使用Invoke? [英] Why use Invoke on Controls in .net?

查看:50
本文介绍了为什么要在.net中的控件上使用Invoke?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
为什么.NET不允许跨线程操作?
为什么只允许UI线程修改用户界面?

据我了解,.net引发非法交叉线程调用异常的原因是GUI可能显示不确定的行为.

From what I have understood, the reason .net raises the illegal cross thread calls exception is that the GUI might show indeterministic behaviour.

但是其他所有对象不是这种情况吗?如果两个线程在同一个对象上工作,则取决于代码,则该对象可能处于不确定状态.那么,为什么控制元素存在此异常?还是为什么此例外是控制元素所独有的.

But isn't this the case with every other object? If two threads work on the same object, depending on the code, the object might be in an indeterministic situation. So why does this exception exist for control elements? Or why is this exception exclusive to control elements.

以及如何使用invoke来提供帮助?它仍然是不确定的.

And how is using invoke going to help? It will still be indeterministic.

推荐答案

调用-需要在其他线程上执行控件,因为

Invoke-ing controls on other threads is required because cross-thread calls to Controls are not allowed. For a more complete discussion as to why this restriction exists you should have a read of that link - I'm not going to answer that here, it just is (however be assured that this restriction exists for a good reason).

调用 Invoke 可以为我们提供帮助,因为它允许后台线程在UI线程上执行操作"-之所以有效,是因为它没有直接调用该方法,而是发送了 Windows消息表示运行当你有机会的时候".UI线程正在运行消息泵,该泵将连续处理发送到该线程的所有消息-通常,这些消息是用户单击了此按钮" ,在这种情况下,Windows Forms通过在相关控件上引发 Click 事件来处理此消息.在这种情况下,Windows窗体通过运行提供的委托来处理消息.

Calling Invoke helps us because it allows a background thread to "do stuff" on a UI thread - it works because it doesn't directly call the method, rather it sends a Windows message that says "run this when you get the chance to". The UI thread is running a message pump which continuously processes all messages sent to that thread - often these messages are things like "the user clicked on this button", in which case Windows Forms handles this message by raising the Click event on the relevant control. In this case Windows Forms handles the message by running the supplied delegate.

结果是在任一时间点(UI线程)只有1个线程正在修改/使用UI控件.

The result is that only 1 thread is modifying / working with the UI controls at any one point in time (the UI thread).

请注意, Invoke 会使没有确保代表将按顺序运行的保证人.如果必须以正确的顺序执行从两个不同线程(甚至是同一线程) Invoked 的两个委托,这一点很重要,那么那就是另一个问题.

Note that Invoke makes no guarentees as to the order in which delegates will be run. If its important that two delegates being Invoked from two different threads (or even the same thread) be executed in the correct order then thats a different problem.

此外:我们谈论"UI线程"是因为大多数应用程序都有一个在其上创建所有控件的线程,但是实际上可以创建不同的控件线程-该控件所在的线程在哪个进程上创建消息.显然,为了正确处理这些消息,必须在该线程上运行消息泵.

Aside: We talk about "the UI thread" because most applications have one thread on which all controls are created, however in reality different controls can be created threads - its the thread that the control was created on which process the message. Obviously in order for those messages to be properly processed there must be a message pump running on that thread.

这篇关于为什么要在.net中的控件上使用Invoke?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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