c# – 为什么WPF中的UI控件具有线程亲和性?

c# – 为什么WPF中的UI控件具有线程亲和性?,第1张

概述为什么创建控件线程是可以更新的线程?为什么MS没有使用锁定和其他线程同步技术来读取和写入具有多个线程的UI控件上的属性的能力. 每个MSDN的简短描述是 WPF’s threading model was kept in sync with the existing User32 threading model of single threaded execution with thread a 为什么创建控件的线程是可以更新的线程?为什么MS没有使用锁定和其他线程同步技术来读取和写入具有多个线程的UI控件上的属性的能力.解决方法 每个MSDN的简短描述是

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

更长的描述是这样的:

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.

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

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.

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.

您可以阅读完整的文章here

就个人而言,我更喜欢WPF的单线程模型,而不必使用锁定和线程同步技术. Dispatcher可用于将消息传递到different priority levels的主UI线程,该线程负责大多数小型后台进程,如果您需要任何繁重的处理,那么您仍然可以为其创建自己的后台线程.

总结

以上是内存溢出为你收集整理的c# – 为什么WPF中的UI控件具有线程亲和性?全部内容,希望文章能够帮你解决c# – 为什么WPF中的UI控件具有线程亲和性?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1259992.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存