.NET控件:为什么不是所有的呼叫线程安全的? [英] .NET Controls: Why aren't all calls thread-safe?

查看:178
本文介绍了.NET控件:为什么不是所有的呼叫线程安全的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在兴奋爆炸在学习如何使线程安全调用Windows窗体控件,它让我想...

After exploding with excitement over learning about how to make thread-safe calls to Windows Form Controls, it got me thinking...

为什么不的所有的调用Windows窗体控件是线程安全的?任何人都可以解释为什么?我认为这将减少很多困惑这些控件的用户。

Why aren't all calls to Windows Form Controls thread-safe? Can anyone explain why? I would think it would reduce a lot of confusion for users of those controls.

推荐答案

这里的问题不是线程安全的。这些方法都是线程安全的,因为它们不会破坏应用程序的多线程同时调用时的状态 - 它只是线程安全的,包括抛出一个错误的线程异常(不记得它叫什么)

The issue here is not one of thread safety. The methods are all "thread safe" in that they do not corrupt the state of the application when called simultaneously on multiple threads - it is just that thread safety includes throwing a wrong thread exception (can't remember what it is called).

什么他们所得到的亲和螺纹 - 它们只能被称为在一个线程 - 有时被称为UI线程,虽然这是一个误导,因为它意味着只有一个。这主要是因为它们依赖于具有相同的线程关联性规则OS调用。

What they have is thread afinity - they can only be called on one thread - sometimes referred to as the UI thread, although this is misleading because it implies there is only one. This is mainly because the OS calls they depend on have the same thread affinity rules.

相信我 - 这是一个的的事情。当你想起UI线程的主要作用这一切开始变得清晰。在UI线程任务是接受输入来自用户的手中,通过键盘或鼠标,作用其上,并产生以像素的形式输出响应。只有一个用户,而用户只有一组的眼睛。用户期望看到他们所做的一切发生在屏幕上,而且最重要的是他们希望看到它发生在他们这样做是顺序。多线程UI使这很难实现。 - 几乎是不可能的

Trust me - this is a good thing. When you think about the primary role of the "UI Thread" it all starts to become clear. The UI threads job is to take input from the users hands, through a keyboard or mouse, act upon it, and produce output in the form of pixels in response. There is only one user, and that user only has one set of eyes. The user expects to see everything they do happen on the screen, and most importantly they expect to see it happen in the order they did it. Multithreaded UI would make this very difficult to achieve - almost impossible.

现在的问题是,当你混你的背景工人线程与UI线程,你需要做的编组交谈的用户界面,因为你必须要在UI线程做在一定的数量。此外,正如我所说,这是一件好事。有人做过这样做编组,否则用户将看到的事情发生在错误的顺序,这是不好的。该系统可以诚然为你做它,并在一些Win32调用它 - 但是这有问题。首先,系统无法知道粒度你需要出现在编组所以你可能最终会被低效率的。您的操作可能会在更高层次上比系统可以了解得到更好的编组。其次,编组是昂贵的,并且惩罚谁在做正确的事和正确地移动了一切向UI线程开发。因此,该系统确实最小的事情就可以了,检查它是否是正确的线程上,如果不成功,就抛出异常。

The problem is that when you mix your background "worker" threads with the UI thread, you need to do a certain amount of marshalling to talk to the UI because you have to be on the UI thread to do it. Again, as I said, this is a good thing. Someone has to do this marshalling, else the user would see things happening in the wrong order and that is bad. The system could admittedly do it for you, and in some WIN32 calls it does - but this has problems. First of all, the system can't know what granularity you need the marshalling to occur at so you might end up being inefficient. Your operations might be better marshalled at a higher level than the system can understand. Secondly, the marshalling is expensive, and it punishes the developers who are doing the right thing and moving everything over to the UI thread correctly. So the system does the minimum thing it can, check if it is on the right thread and if not, throw an exception.

这篇关于.NET控件:为什么不是所有的呼叫线程安全的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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