WPF Dispatcher管理线程工作队列

WPF中主线程和UI线程

WPF和winform 中经常听到UI线程的概念,调试后发现创建UI控件的线程就是主线程。所以对于WPF来说UI线程就是主线程。UI线程相对应的概念是工作者线程,主线程相对的概念是子线程,线程的划分方法不同而已。由于UI线程有消息循环而工作者线程则没有,所以工作者线程也无法获取到消息,这样的话程序为了响应各种消息,所以必须用UI线程作为主线程。

ui线程和工作线程是人为划分的,当操作系统建立线程时,会假定线程不会被用户和用户交互。这样可以减少线程对系统资源的要求。但是,一旦这个线程调用一个与图形界面有关的函数,比如GetMessage或者CreateWindow,系统就会为这个线程分配很多资源,其中就包括消息队列,这个时候这个线程可以成为ui线程吧。

Dispatcher 作用

不管是WinForm应用程序还是WPF应用程序,实际上都是一个进程,一个进程可以包含多个线程,其中有一个是主线程,其余的是子线程。在WPF或WinForm应用程序中,主线程负责接收输入、处理事件、绘制屏幕等工作,为了使主线程及时响应,防止假死,在开发过程中对一些耗时的操作、消耗资源比较多的操作,都会去创建一个或多个子线程去完成操作,比如大数据量的循环操作、后台下载。由于UI界面是主线程创建的,所以子线程不能直接更新由主线程维护的UI界面。

Dispatcher的作用是用于管理线程工作项队列,类似于Win32中的消息队列,Dispatcher的内部函数,仍然调用了传统的创建窗口类,创建窗口,建立消息泵等操作。Dispatcher本身是一个单例模式,构造函数私有,暴露了一个静态的CurrentDispatcher方法用于获得当前线程的Dispatcher。

对于线程来说,它会按照一定的优先级顺序依次执行不同的方法(委托实例)。而dispatcher 就是设置不同方法(委托实例)的优先级的。对于WPF控件来说,由于WPF的UI是主线程创建的,所以控件的dispatcher 实际就是设置主线程执行的优先级。

define in MSDN

The Dispatcher maintains a prioritized queue of work items for a specific thread.

When a Dispatcher is created on a thread, it becomes the only Dispatcher that can be associated with the thread, even if the Dispatcher is shut down.

If you attempt to get the CurrentDispatcher for the current thread and a Dispatcher is not associated with the thread, a Dispatcher will be created. A Dispatcher is also created when you create a DispatcherObject. If you create a Dispatcher on a background thread, be sure to shut down the dispatcher before exiting the thread.

In WPF, a DispatcherObject can only be accessed by the Dispatcher it is associated with. For example, a background thread cannot update the contents of a Button that is associated with the Dispatcher on the UI thread. In order for the background thread to access the Content property of the Button, the background thread must delegate the work to the Dispatcher associated with the UI thread. This is accomplished by using either Invoke or BeginInvoke. Invoke is synchronous and BeginInvoke is asynchronous. The operation is added to the queue of the Dispatcher at the specified DispatcherPriority.

METHODS
BeginInvoke(Delegate, DispatcherPriority, Object[])
Executes the specified delegate asynchronously with the specified arguments, at the specified priority, on the thread that the Dispatcher was created on.

Invoke(Action)
Executes the specified Action synchronously on the thread the Dispatcher is associated with.

instance


startStopButton.Dispatcher.BeginInvoke(
    DispatcherPriority.Normal,
    new NextPrimeDelegate(CheckNextNumber));

WPF 类型继承

按照类的继承关系依次读一下MSDN的描述

在 WPF 的类层次结构中,大部分都集中派生于 DispatcherObject 类(通过其他类)。如下图 所示,您可以看到 DispatcherObject 虚拟类正好位于 Object 下方和大多数 WPF 类的层次结构之间。 要了解他们之间的关系可以参看下面这张类继承关系图:
在这里插入图片描述
对上图的一些说明:

  1. System.Object 类:大家都知道在.Net中所有类型的基类,DispatcherObject 就继承于它,所以它是WPF的基类。

  2. System.Windows.Threading.DispatcherObject 类:从图中看WPF 中的使用到的大部分控件与其他类大多是继承 DispatcherObject 类,它提供了用于处理并发和线程的基本构造。

  3. System.Windows.DependencyObject类:对WPF中的依赖项属性承载支持与 附加属性承载支持,表示参与 依赖项属性 系统的对象。

  4. System.Windows.Media.Visual类:为 WPF 中的呈现提供支持,其中包括命中测试、坐标转换和边界框计算等。

  5. System.Windows.UIElement 类:UIElement 是 WPF 核心级实现的基类,该类是 Windows Presentation Foundation (WPF) 中具有可视外观并可以处理基本输入的大多数对象的基类。

  6. System.Windows.FrameworkElement类:为 Windows Presentation Foundation (WPF) 元素提供 WPF 框架级属性集、事件集和方法集。此类表示附带的 WPF 框架级实现,它是基于由UIElement定义的 WPF 核心级 API 构建的。

  7. System.Windows.Controls.Control 类:表示 用户界面 (UI) 元素的基类,这些元素使用 ControlTemplate 来定义其外观。

  8. System.Windows.Controls.ContentControl类:表示没有任何类型的内容表示单个控件。

WPF的绝大部分的控件,还包括窗口本身都是继承自ContentControl的。

ContentControl族包含的控件
在这里插入图片描述
9) System.Windows.Controls.ItemsControl 类:表示可用于提供项目的集合的控件。

以条目集合位内容的控件 ItemsControl

特点: a.均派生自ItemsControl

       b.内容属性为Items或ItemsSource

        c.每种ItemsControl都对应有自己的条目容器(Item Container).

ItemsControl族包含的控件

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值