为什么在WPF UI控件有线程关联? [英] Why do UI Controls in WPF have Thread Affinity?

查看:69
本文介绍了为什么在WPF UI控件有线程关联?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么是它创建该控件的线程是可更新的人吗?为什么没有MS会给人使用锁定和阅读其他线程同步技术和写入性能上的UI多线程控制的能力。

Why is it that the thread that created the control is the one that can update it? Why didn't MS give people the ability to use locking and other thread synchronization techniques for reading and writing to properties on UI controls with multiple threads.

推荐答案

每MSDN简短的介绍

WPF的线程模型保持同步与现有USER32
线程模型与线程关联单线程执行。在
首要原因,这是互操作性 - 系统,如OLE 2.0,
剪贴板,和Internet Explorer都需要单独的线程
亲和力(STA)执行

WPF’s threading model was kept in sync with the existing User32 threading model of single threaded execution with thread affinity. The primary reason for this was interoperability – systems like OLE 2.0, the clipboard, and Internet Explorer all require single thread affinity (STA) execution

较长的描述是这样的:

在WPF中的大多数对象自DispatcherObject,推导出提供了
基本结构处理的并发线程和。 WPF是基于由调度员实施了邮件系统上
。此工程
很像熟悉的Win32消息泵;事实上,WPF调度员
使用USER32消息进行跨线程调用。

Most objects in WPF derive from DispatcherObject, which provides the basic constructs for dealing with concurrency and threading. WPF is based on a messaging system implemented by the dispatcher. This works much like the familiar Win32 message pump; in fact, the WPF dispatcher uses User32 messages for performing cross thread calls.

实际上有两个核心概念在讨论
并发时,理解WPF - 调度和线程关联

There are really two core concepts to understand when discussing concurrency in WPF – the dispatcher and thread affinity.

在WPF的设计阶段,目标是移动到一个单独的执行
线程,但非线程affinitized的模式。当一个组件使用执行
线程的身份来存储某些类型的状态的线程
亲和力发生。这方面最常见的形式是
使用线程本地存储(TLS)来存储状态。螺纹亲和力
要求执行的每个逻辑线程仅由一个
在操作系统中,它可以成为存储器
密集物理线程拥有。最终,WPF的线程模型保持同步与
与现有的
线程关联单线程执行USER32线程模型。造成这种情况的主要原因是互操作性 - 就像OLE 2.0,剪贴板和Internet Explorer
系统都需要
单线程亲和力(STA)执行

During the design phase of WPF, the goal was to move to a single thread of execution, but a non-thread "affinitized" model. Thread affinity happens when a component uses the identity of the executing thread to store some type of state. The most common form of this is to use the thread local store (TLS) to store state. Thread affinity requires that each logical thread of execution be owned by only one physical thread in the operating system, which can become memory intensive. In the end, WPF’s threading model was kept in sync with the existing User32 threading model of single threaded execution with thread affinity. The primary reason for this was interoperability – systems like OLE 2.0, the clipboard, and Internet Explorer all require single thread affinity (STA) execution.

既然你有STA线程对象,您需要一种方法来
线程之间的沟通,并验证你是正确的
线程。就在于此调度的作用。调度员是
基本信息调度系统,拥有多个优先级队列。消息
实例包括原始输入通知(鼠标移动),
框架功能(布局),或者用户命令(执行此方法)。
通过自DispatcherObject派生,创建具有
STA行为CLR对象,并在创建
时间给出一个指向一个调度器。

Given that you have objects with STA threading, you need a way to communicate between threads, and validate that you are on the correct thread. Herein lies the role of the dispatcher. The dispatcher is a basic message dispatching system, with multiple prioritized queues. Examples of messages include raw input notifications (mouse moved), framework functions (layout), or user commands (execute this method). By deriving from DispatcherObject, you create a CLR object that has STA behavior, and will be given a pointer to a dispatcher at creation time.

您可以在这里一>

You can read the full article here

我个人更喜欢WPF的过去要使用锁和线程同步技术单线程模型。 href=\"http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcher.aspx\">调度可以用来在信息传递给主UI线程的不同的优先级的,这需要广大小的后台进程的护理,如果您需要任何繁重的处理,那么你仍然可以创建你自己的后台线程。

Personally I prefer WPF's single-threaded model over having to use locking and thread synchronization techniques. The Dispatcher can be used to pass messages to the main UI thread at different priority levels, which takes care of the majority of small background processes, and if you need any heavy processing then you can still create your own background thread for that.

这篇关于为什么在WPF UI控件有线程关联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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