Dispatcher 类提供用于管理线程工作项队列的服务。

效果演示:

<Window x:Class="WPF之Dispatcher对象.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="" Width="">
<Grid Background="BurlyWood" Height="" Width="">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="184*" />
<ColumnDefinition Width="172*" />
<ColumnDefinition Width="147*" />
</Grid.ColumnDefinitions> <Label Content="vlue:0" FontSize="" Height="" Margin="125,10,162,0" Name="label1" VerticalAlignment="Top" Grid.ColumnSpan="" />
<Button Content="Dispatcher 回调界面线程" Height="" Margin="125,0,117,157" Name="button1" VerticalAlignment="Bottom" Grid.ColumnSpan="" Click="button1_Click" />
<Button Content="直接更新UI界面线程" Height="" Margin="125,0,117,209" Name="button2" VerticalAlignment="Bottom" Grid.ColumnSpan="" Click="button2_Click" />
<Button Content="同步更新UI界面线程" Height="" Margin="125,0,117,105" Name="button3" VerticalAlignment="Bottom" Grid.ColumnSpan="" Click="button3_Click" />
<Button Content="异步更新UI界面线程" Height="" Margin="125,0,117,53" Name="button4" VerticalAlignment="Bottom" Grid.ColumnSpan="" Click="button4_Click" />
<ProgressBar Height="" Margin="125,97,0,0" Name="progressBar1" VerticalAlignment="Top" Grid.ColumnSpan="" HorizontalAlignment="Left" Width="" />
<Label Content="Dispatcher 应用" FontSize="" Height="" Margin="157,-142,135,0" Name="label3" VerticalAlignment="Top" Grid.ColumnSpan="" />
</Grid>
</Window>

后台简答代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading; namespace WPF之Dispatcher对象
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
} private void button2_Click(object sender, RoutedEventArgs e)
{
for (int i = ; i <= ; i++)
{
this.label1.Content = "value:" + i+"%";
this.progressBar1.Value = i;
System.Threading.Thread.Sleep(); //不能引用 System.Windows.Forms中的DoEvents会降低性能
System.Windows.Forms.Application.DoEvents(); }
} private void button1_Click(object sender, RoutedEventArgs e)
{
for (int i = ; i <= ; i++)
{
this.label1.Content = "value:" + i+"%";
this.progressBar1.Value = i;
System.Threading.Thread.Sleep();
this.DoEvents();//调用扩展方法DoEvents刷新界面同步
}
}
public delegate void delegateUI(string val);
public void RefashUI(string val)
{
this.label1.Content = "value:" + val+"%";
this.progressBar1.Value = int.Parse(val); } private void button3_Click(object sender, RoutedEventArgs e)
{
for (int i = ; i <= ; i++)
{
this.Dispatcher.Invoke(new delegateUI(RefashUI), //同步执行
DispatcherPriority.Normal, //优先级设置
new string[] { i.ToString() });
System.Threading.Thread.Sleep();
this.DoEvents();
} }
private void button4_Click(object sender, RoutedEventArgs e)
{
for (int i = ; i <= ; i++)
{
this.Dispatcher.BeginInvoke(new delegateUI(RefashUI),//异步执行
DispatcherPriority.Normal,
new string[] { i.ToString() });
System.Threading.Thread.Sleep();
this.DoEvents(); //也可以用这种方法启动线程,设置线程优先级
//this.Dispatcher.Thread.Start();
//this.Dispatcher.Thread.Priority = System.Threading.ThreadPriority.Highest;
}
}
} //一个扩展的自定义DoEvents,可以直接使用我们的项目中
public static class WindowEX
{
public static void DoEvents(this MainWindow win)
{
DispatcherFrame frame = new DispatcherFrame();
Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background,
new DispatcherOperationCallback(ExitFrames), frame);
Dispatcher.PushFrame(frame);
} public static object ExitFrames(object f)
{
((DispatcherFrame)f).Continue = false;
return null;
} }
}

上面展示了Dispatcher对象简单的运用,更多的用法http://msdn.microsoft.com/zh-cn/library/vstudio/System.Windows.Threading.Dispatcher.aspx

下载:http://files.cnblogs.com/BABLOVE/WPF%E4%B9%8BDispatcher%E5%AF%B9%E8%B1%A1.rar

WPFDispatcher示例的更多相关文章

  1. Swift3.0服务端开发(一) 完整示例概述及Perfect环境搭建与配置(服务端+iOS端)

    本篇博客算是一个开头,接下来会持续更新使用Swift3.0开发服务端相关的博客.当然,我们使用目前使用Swift开发服务端较为成熟的框架Perfect来实现.Perfect框架是加拿大一个创业团队开发 ...

  2. .NET跨平台之旅:将示例站点升级至 ASP.NET Core 1.1

    微软今天在 Connect(); // 2016 上发布了 .NET Core 1.1 ,ASP.NET Core 1.1 以及 Entity Framework Core 1.1.紧跟这次发布,我们 ...

  3. 通过Jexus 部署 dotnetcore版本MusicStore 示例程序

    ASPNET Music Store application 是一个展示最新的.NET 平台(包括.NET Core/Mono等)上使用MVC 和Entity Framework的示例程序,本文将展示 ...

  4. WCF学习之旅—第三个示例之四(三十)

           上接WCF学习之旅—第三个示例之一(二十七)               WCF学习之旅—第三个示例之二(二十八)              WCF学习之旅—第三个示例之三(二十九)   ...

  5. JavaScript学习笔记(一)——延迟对象、跨域、模板引擎、弹出层、AJAX示例

    一.AJAX示例 AJAX全称为“Asynchronous JavaScript And XML”(异步JavaScript和XML) 是指一种创建交互式网页应用的开发技术.改善用户体验,实现无刷新效 ...

  6. XAMARIN ANDROID 二维码扫描示例

    现在二维码的应用越来越普及,二维码扫描也成为手机应用程序的必备功能了.本文将基于 Xamarin.Android 平台使用 ZXing.Net.Mobile  做一个简单的 Android 条码扫描示 ...

  7. iOS之ProtocolBuffer搭建和示例demo

    这次搭建iOS的ProtocolBuffer编译器和把*.proto源文件编译成*.pbobjc.h 和 *.pbobjc.m文件时,碰到不少问题! 搭建pb编译器到时没有什么问题,只是在把*.pro ...

  8. Android种使用Notification实现通知管理以及自定义通知栏(Notification示例四)

    示例一:实现通知栏管理 当针对相同类型的事件多次发出通知,作为开发者,应该避免使用全新的通知,这时就应该考虑更新之前通知栏的一些值来达到提醒用户的目的.例如我们手机的短信系统,当不断有新消息传来时,我 ...

  9. oracle常用函数及示例

    学习oracle也有一段时间了,发现oracle中的函数好多,对于做后台的程序猿来说,大把大把的时间还要学习很多其他的新东西,再把这些函数也都记住是不太现实的,所以总结了一下oracle中的一些常用函 ...

随机推荐

  1. Spring技术内幕——深入解析Spring架构与设计原理(一)IOC实现原理

    IOC的基础 下面我们从IOC/AOP开始,它们是Spring平台实现的核心部分:虽然,我们一开始大多只是在这个层面上,做一些配置和外部特性的使用工作,但对这两个核心模块工作原理和运作机制的理解,对深 ...

  2. 使用CLRMD编写一个自己的C#调试器

    博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:使用CLRMD编写一个自己的C#调试器.

  3. 微信小程序开发体验

    1.  申请小程序账号 小程序目前不支持个人申请,企业申请后填写基本信息 本来以为用原来公司申请的公众号就可以申请小程序权限,貌似不行 2.  添加开发者 管理员默认拥有开发者所有权限 添加其他开发者 ...

  4. MVC 音乐商店 第 9 部分: 注册和结帐

    MVC 音乐商店是介绍,并分步说明了如何使用 ASP.NET MVC 和 Visual Studio 为 web 开发教程应用程序. MVC 音乐商店是一个轻量级的示例存储实现它卖音乐专辑在线,并实现 ...

  5. iOS 利用Socket UDP协议广播机制的实现

    1.前言 什么是UDP协议广播机制? 举一个例. 比如在一群人群中,一个人要找张三,于是你向人群里大喊一声(广播):"谁是张三" 假设它是张三,它就会回应你.在网络中也是一样的. ...

  6. SQL Server与Oracle对比学习:表的管理和组织

    http://blog.csdn.net/weiwenhp/article/details/8088979 我们知道数据库,顾名思义.最重要的东东就是管理数据,而数据在系统中主要是保存在表(table ...

  7. the Linux Kernel: Traffic Control, Shaping and QoS

    −Table of Contents Journey to the Center of the Linux Kernel: Traffic Control, Shaping and QoS 1 Int ...

  8. MySQLdb安装和使用2

    http://blog.chinaunix.net/uid-8487640-id-3183185.html MySQLdb是Python连接MySQL的模块,下面介绍一下源码方式安装MySQLdb: ...

  9. jsp-javabean-setproperty介绍

    李兴华<java web开发实战经典>第7章关于javabean的讲解中说道:<jsp:setProperty>标签一共有4种使用方法·下面列出了4种操作语法的格式:   设置 ...

  10. Performing a thread dump in Linux or Windows--reference

    Linux/Unix 1. Find the Java/Tomcat process id. % ps ax | grep java You should see output like this 1 ...